How to get all posts related to particular category name?

I am developing one project and in this project i have to display all the posts related to particular category name.

I have searched a lot but i haven’t got any idea to implement this.

Read More

How can i do this so that i can display all the posts from particular category/term

Related posts

Leave a Reply

6 comments

  1. Just use WP_Query() to generate your custom query, using the category parameters.

    Assuming you know (or know how to get) the ID of the specific category, as $catid:

    <?php
    $category_query_args = array(
        'cat' => $catid
    );
    
    $category_query = new WP_Query( $category_query_args );
    ?>
    

    Note: you could also pass the category slug to the query, via category_name, instead of cat.

    Now, just output your loop:

    <?php
    if ( $category_query->have_posts() ) : while $category_query->have_posts() : $category_query->the_post();
    // Loop output goes here
    endwhile; endif;
    ?>
    
  2. Below code will fetch post title from particular category name.

     <?php 
          $myposts = get_posts(array(
          'showposts' => 8, //add -1 if you want to show all posts
          'post_type' => 'your-post-type',
          'tax_query' => array(
                      array(
                            'taxonomy' => 'your-taxonomy',
                            'field' => 'slug',
                            'terms' => 'term-name' //pass your term name here
                              )
                            ))
                           );
    
            foreach ($myposts as $mypost) {
            // echo $mypost->post_title . '<br/>';
            // echo $mypost->post_content . '<br/>';
            // echo  $mypost->ID . '<br/><br/>';
            echo '<li class="faq"> <p class="title"><a href="' . get_permalink($mypost) . '">' . $mypost->post_title . '</a></p></li>';} ?>
    
  3. WP_Query‘s tax_query is, far and away, going to be the most flexible way to implement this. If you make the question a bit more specific, I should be able to crank out some sample code for you to get you going.

  4. You can use a plugin (WordPress Category Posts) for that.

    WordPress Category Posts is a plugin for WordPress that creates a linked list of the posts in a specific category.

    Use the following code wherever you want to list the posts for a category:

    wp_cat_posts(get_cat_ID('your_category_name'));
    

    Many thanks.