Get Posts From Multiple Post Types and Multiple Categories

Posted by Will on August 7, 2012

This is a solution to a recent problem I was having getting the right data out of WordPress with their built in get_posts() function. I was dealing with the default post type in WordPress, which is ‘post’, and I was dealing with a custom post type called ‘blogs’. I had a featured category for the regular post type, and then I had a featured category in a custom taxonomy. I wanted to pull one list from both post types, and both categories. Just specifying the categories doesn’t work, I believe because it looks for only categories, and not custom taxonomy categories. Here was the code that worked for me:

$args = array(    //set the arguments to pass to the functions
    'post_type'=> array('post','blogs'),
    'genre'=>array('featured blogs','homepage featured')

$slider_posts = get_posts( $args ); 

foreach($slider_posts as $slider_post){   //loop through the posts and display
     echo $slider_post->post_title;

And there you have it.

Leave a Reply

Your email address will not be published. Required fields are marked *