Query posts and then add them to the main query using pre_get_posts?

My test query is setup to show 15 posts and then use loops to separate 5 posts that use the category podcast-control-daily. It works fine until I write the 16th post and then the last post from podcast-control-daily category no longer gets queried, so it only shows 4 posts.

I’m currently using pre_get_posts to query posts without those using the category podcast-control-daily in the main_query and then a second query for those posts using podcast-control-daily. Is there a way to do another query and then add those posts to the main_query. I’m trying to have my website only use one query instead of two.

pre_get_posts query:

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set(‘tax_query’, array(array(‘taxonomy’ => ‘category’,’field’ => ‘slug’,’terms’ => array( ‘podcast-control-daily’ ),’operator’=> ‘NOT IN’),array(‘taxonomy’ => ‘post_format’,’field’ => ‘slug’,’terms’ => array( ‘post-format-status’ ),’operator’=> ‘NOT IN’)));
$query->set( ‘posts_per_page’, 15 );
}
return $query;}
add_action( ‘pre_get_posts’, ‘exclude_category’ );
function ctrl_daily_posts( $query2 ){
if( $query2->get( ‘ctrl_daily’ ) == ‘posts’ ){
$query2->set( ‘tax_query’, array(array(‘taxonomy’ => ‘category’,’field’ => ‘slug’,’terms’ => array( ‘podcast-control-daily’ ),’operator’=> ‘IN’)));
$query2->set( ‘posts_per_page’, 5 );
}
return $query2;}

Read more here:: Query posts and then add them to the main query using pre_get_posts?

Leave a Reply

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