I need to modify the main loop (index.php) to exclude posts that have a particular meta value set (via an ACF True / False field).
function exclude_featured_post( $query ) {
if ( $query->is_home() && $query->is_main_query()) {
// in case for some reason there's already a meta query set from other plugin
$meta_query = $query->get('meta_query')? : [];
// append yours
$meta_query[] = [
'key' => 'featured_post',
'value' => '1',
'compare' => '!='
];
$query->set('meta_query', $meta_query);
}
}
add_action( 'pre_get_posts', 'exclude_featured_post' );
This returns no posts. I am thinking I need to merge this with the main query, something like:
global $wp_query;
$merged_args = array_merge( $wp_query->query, $exclude_featured_args );
$query->set('meta_query', $merged_args);
However, this also returns no posts.
Update:
Using 'compare' => 'NOT EXISTS` instead of 'compare' => '!=' seems to return all posts and exclude the meta_query.
function exclude_featured_post( $query ) {
if ( $query->is_home() && $query->is_main_query()) {
// in case for some reason there's already a meta query set from other plugin
$meta_query = $query->get('meta_query')? : [];
// append yours
$meta_query[] = [
'key' => 'featured_post',
'value' => '1',
'compare' => 'NOT EXISTS'
];
$query->set('meta_query', $meta_query);
}
}
add_action( 'pre_get_posts', 'exclude_featured_post' );
From here: https://core.trac.wordpress.org/ticket/18158