Adam Katz
Adam Katz

Reputation: 6962

Get posts that have both categories

I am trying to make an array that holds all the pages of the site that have both the category big as well as insect.

Currently it looks like this

<?php
$argsposts = array(
    'category_name' => 'big'
); 
$posts = get_posts($argsposts);
?>

And it successfully gets all the posts with the category big however I would like to update it to only get posts with the category big if they also have the category insect.

Upvotes: 1

Views: 150

Answers (2)

rnevius
rnevius

Reputation: 27092

You can use the following:

$query = new WP_Query( 'category_name=big+insect' );

This is right in the docs.

Display posts that have "all" of these categories.

Upvotes: 1

vard
vard

Reputation: 4136

Try this :

$big_cat = get_category_by_slug('big');
$insect_cat = get_category_by_slug('insect');
$argsposts = array(
  'category__and' => array($big_cat->term_id, $insect_cat->term_id)
  );
$posts = get_posts($argsposts);

Upvotes: 0

Related Questions