Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carlaizumibamford/5fa57de98a74279521f1f270e0bfd11f to your computer and use it in GitHub Desktop.
Save carlaizumibamford/5fa57de98a74279521f1f270e0bfd11f to your computer and use it in GitHub Desktop.
Display posts that have one category (and any children of that category), using category id:
$query = new WP_Query( array( 'cat' => 4 ) );
Display posts that have this category (and any children of that category), using category slug:
$query = new WP_Query( array( 'category_name' => 'staff' ) );
Display posts that have this category (not children of that category), using category id:
$query = new WP_Query( array( 'category__in' => 4 ) );
Display posts that have several categories, using category id:
$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );
Display posts that have these categories, using category slug:
$query = new WP_Query( array( 'category_name' => 'staff,news' ) );
Display posts that have “all” of these categories:
$query = new WP_Query( array( 'category_name' => 'staff+news' ) );
Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.
$query = new WP_Query( array( 'cat' => '-12,-34,-56' ) );
Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):
$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );
You can also exclude multiple categories this way:
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment