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,

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

Would love to hear any insight you might have


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 

Leave a Comment