how to query posts by category and tag?

  • I am trying to show a list of posts that are related to category X and tag Y. I've tried the following code:

    $args = array(
        'posts_per_page' => 4,
        'tag_id' => $tag_id,
        'cat' => $cat_id,
    );
    query_posts($args);
    

    but it doesn't work correctly and returns all the posts in the co\ategory.

    Would love to hear any insight you might have

    I think with query_posts() you can only make use of category or tag. I'm not sure, but maybe the use of the function is limited to that which would mean that this is correctly working but it doesn't do what you want to do it.

  • Chris_O

    Chris_O Correct answer

    10 years ago

    Edit: See below for proper way to query category and tag intersections.

    global $wp_query;
            $args = array(
            'category__and' => 'category', //must use category id for this field
            'tag__in' => 'post_tag', //must use tag id for this field
            'posts_per_page' => -1); //get all posts
    
    $posts = get_posts($args);
            foreach ($posts as $post) :
      //do stuff 
         endforeach;
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM