We can try the following alternative way:
-
Remove the two posts we select via our custom query from the main query via the
pre_get_posts
action -
Return the two posts on top of page one via the
the_posts
filter
Lets look at possible code:
add_action( 'pre_get_posts', function ( $q )
{
remove_filter( current_filter(), __FUNCTION__ );
if ( $q->is_home() // Only target the home page
&& $q->is_main_query() // Only target the main query
) {
// Set our query args and run our query to get the required post ID's
$args = [
'meta_key' => 'caja',
'meta_value' => ['uno','dos'],
'posts_per_page' => '2',
'fields' => 'ids', // Get only post ID's
];
$ids = get_posts( $args );
// Make sure we have ID's, if not, bail
if ( !$ids )
return;
// We have id's, lets remove them from the main query
$q->set( 'post__not_in', $ids );
// Lets add the two posts in front on page one
if ( $q->is_paged() )
return;
add_filter( 'the_posts', function ( $posts, $q ) use ( $args )
{
if ( !$q->is_main_query() )
return $posts;
// Lets run our query to get the posts to add
$args['fields'] = 'all';
$posts_to_add = get_posts( $args );
$stickies = get_option( 'sticky_posts' );
if ( $stickies ) {
$sticky_count = count( $stickies );
array_splice( $posts, $sticky_count, 0, $posts_to_add );
return $posts;
}
// Add these two posts in front
$posts = array_merge( $posts_to_add, $posts );
return $posts;
}, 10, 2 );
}
});
This should replace the current code that you have posted in your question