WordPress.org

Ready to get started?Download WordPress

Forums

How to meta_query dates using WP_Query (1 post)

  1. Tokant
    Member
    Posted 10 months ago #

    Hi!

    I have made two custom post types, "exhibitions" and "events".

    For "exhibitions" there are two custom fields (meta_key): startdate and enddate.
    For "events" there is one custom field (meta_key): date.

    I want to use php's date() and WP_Query to show all "exhibitions" and "events" that are on today. Thus i need to query wether today's date is within the range of startdate and enddate or on date.

    To find today's date i use:

    date_default_timezone_set('Europe/Copenhagen');
    $todaysdate = strtotime(date("Y-m-d"));

    My WP_Query so far:

    $args = array(
    'post_type' => array( 'exhibitions', 'events' ),
    'meta_query' => array(
    array(
    'key' => 'wpcf-date',
    'value' => '$todaysdate',
    'compare' => 'LIKE'
    )
    )
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    echo 'hello';
    }
    }
    wp_reset_postdata();

    How do adjust the meta_query to also show exhibitions where today's date is between the start- and enddate?

    Thanks!

Reply

You must log in to post.

About this Topic