Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On my main page, I want to show posts from 3 categories, and 1 custom post type.

Is there a way to get them together in one pre_get_posts function? Or do I need to query separately - once for the CPT and once for the posts from specific categories?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you are after on post from category a, one from category b, another form category c and then finally a custom post type - each of these would have to be a separate query.

Think of these as 'secondary queries' - (the primary query being what lands you on the home page). So you'll want use seperate instances of WP_Query (see this related post).

E.g.

$post_from_cat_a  = new WP_Query(array( 
    'category__name' => array('a'), 
    'posts_per_page'=> 1
));
if( $post_from_cat_a->have_posts() ){
    while( $post_from_cat_a->have_posts() ): $post_from_cat_a->the_post();
         //Display output here
    endwhile;
}

$post_from_cat_b  = new WP_Query(array( 
    'category__name' => array('b'), 
    'posts_per_page'=> 1
));
if( $post_from_cat_b->have_posts() ){
  ...
  ...

etc. Don't forget to call wp_reset_postdata(); at the end.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.