1

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?

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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