Filter Posts By Category Wordpress

Filter posts category wise

Filter Posts By Category WordPress

Filter Posts By Category WordPress

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.

Tags:

4 comments

  1. HindIndia
    Reply

    Very helping article …. from this we can get real assistance. 🙂

    • admin
      Reply

      Thank you so much 🙂 🙂

  2. Zorawar
    Reply

    thanq guru…!!!

    • admin
      Reply

      Thank you so much.
      keep visiting 🙂

Leave a Reply

most readable posts

Filter Posts By Category WordPress

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 […]

Read more
Get Custom Post Type With WP_Query In WordPress

In this post I’ll give you an in-depth look into how WP_Query works – let’s get started! This series on WP_Query, you’ll learn how to use WP_Query to query for posts, pages and custom post types. You can query for specific posts and pages or you can run a query to return posts of one […]

Read more
How To Get Post By Taxonomy In WordPress

Taxonomy are basically a way of grouping data in WordPress. The most common default taxonomy used in WordPress are when grouping posts as either ‘categories’ or ‘tags’ and these are named and recorded in the WordPress database as ‘category’ and ‘post_tag’ respectively. Specific categories or tags are called terms. You can see this image for […]

Read more

About Me

We are glad to see you here… If you are visiting for the first time, then, we hope you’ll stay with us in Touch. Codinglead is all about sincere blogging and spreading some basic and some advanced knowledge to you All. As you can see that here that codinglead.com is working on many different categories and concepts (check out in the Menu).

Our Expertise

  • We are providing the resources of the following technologies:
  • Web Development
  • Web Programming
  • PHP/MySQL
  • Know more.