0

I have created a real estate theme, and for each property there is a property listing expiry date after which the property no longer shows on the site. Each property is a custom Properties post type. The code I use to only use live properties is;

function live_properties_only( $query ) {

    if ( ! is_main_query() ) {

        $today = date('Ymd');
        $meta_query = array (
            'post_type'      => 'properties',
            'meta_query' => array(
                    'key'       => 'date_listing_expires',
                    'compare'   => '>',
                    'value'     => $today,
                ),
        );

    $query->set('meta_query',$meta_query);

    }
}

add_action( 'pre_get_posts', 'live_properties_only' );

This is in the functions.php file, the only problem is that is prevents regular blog posts from showing. How can I resolve this so blog posts show and only un-expired properties also show.

  • Try adding wp_reset_query(); after your if statement – Spartacus Feb 10 '17 at 17:00
1

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'
                ),
        );
0

It seems you are only querying only for posts of the type 'properties.' I'm not 100% sure, but removing the post_type line may resolve this. I hope this helps :)

$meta_query = array (
            'meta_query' => array(
                    'key'       => 'date_listing_expires',
                    'compare'   => '>',
                    'value'     => $today,
                ),
        );

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.