I think you need to check if you are in the properties
post type archive before to manipulate the query, so it doesn’t affect to other archives.
Also, you need to check if the actual $query
is the main query, so other queries are not affected (the function is_main_query()
does a different thing; it doesn’t check actual query; if you turn DEBUG on, you will get a message in the logs saying you are doing it wrong by using is_main_query()
in pre_get_posts
action). Finally, you probably want to exclude the query manipulation in admin side:
function live_properties_only( $query ) {
if ( is_post_type_archive( 'properties' ) && ! is_admin() && $query->is_main_query() ) {
$today = date('Ymd');
$meta_query = array (
'meta_query' => array(
'key' => 'date_listing_expires',
'compare' => '>',
'value' => $today,
),
);
$query->set('meta_query',$meta_query);
}
}
add_action( 'pre_get_posts', 'live_properties_only' );
PD: comparing dates in meta fields should use YYYY-MM-DD format (Y-m-d for PHP date()
function); then you can set the parameter 'type' => DATE
and you will be doing a date comparison in the way it has been tested; other methods can work but WP_Query
has not been tested with them (obviously, the meta fields values need to be stored in this format as well):
$today = date('Y-m-d');
$meta_query = array (
'meta_query' => array(
'key' => 'date_listing_expires',
'compare' => '>',
'value' => $today,
'type' => 'DATE'
),
);