If you have lots of posts in your WordPress site, you have probably used the Category and/or Tag filter at the top of the post. These filter are great because they very quickly allow you to limit the kind of posts that are displayed, and let you find the one(s) you are looking for with relative ease. Well, if you use any filter post types or taxonomies, then you have probably noticed that these options are not available. So I’m going to show how to add new filter for your post taxonomies to any post type you have registered on your site.
The process is very simple, in that it does not require a lot of code for filter, and WordPress does most of the work for you, but the function itself is a little bit complex. What we are going to do is setup a function that creates a drop down select menu based off of the terms in the taxonomies we provide for filter. So if we set the function to display a filter for category, it will retrieve a list of all of the terms in that category (all terms that are not empty), and then display each of those post. If we provide multiple category, through an array, then the function will get the filter post of thats category.
If you want to display filter posts from a single category in your WordPress theme, you should add the following line:
<?php $args = array( 'cat' => 2 ); $query = new WP_Query( $args ); if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <div> <h2><?php the_title(); ?></h2> <?php the_content(); ?> </div> <?php endwhile; wp_reset_postdata(); ?> <!-- show pagination here --> <?php else : ?> <!-- show 404 error here --> <?php endif; ?>
Replace 2 with your category ID. This will filter the Look showing only posts from the category you have selected.
If you want to filter more than one category, you can add the IDs separated with comas like this :
$args = array( 'category__and' => array( 2, 6 ) );
and call same as upper code WP_Query query.