Wordpress - get_posts with multiple categories

As documented in the codex you could use the following

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) ); //post has to be in category with ID 2 AND 6


The Codex says:

Note: The category parameter needs to be the ID of the category, and not the category name.

Note: The category parameter can be a comma separated list of categories, as the get_posts() function passes the 'category' parameter directly into WP_Query as 'cat'.

So it'd be same as WP_Query()'s Category parameter - cat.


You can use the WP_QUERY class to search for the posts in some specific categories, here is an example:

$query = new WP_Query( 'cat=2,6,17,38' );

Here is a link to the documentation where they explain how to use the results to build a loop and display the posts in the results.