Filter dates using Wp_query and Post meta in Wordpress

Asked

Viewed 533 times

1

I am developing an event category in my template using the default post, in this post I added two fields (Metabox) for the event start date and end date. Already in Wp_query I need to display the events that have the initial date less than the current date, and the shorter end date.

Using Wp_query or get_post_meta, or any other function... How can I do this?

  • 1

    How are you saving the date? In what format?

  • Date format: d/m/Y

  • Honestly this is the worst way to save, if save as Ymd is quiet to make the queries.

  • Yes, but my problem is how to do the consultation, because I don’t know.

1 answer

2


In case it depends on the way you save the date... I recommend using the format:

YmdHis

This way you can make a very simple query by buying numbers only:

'meta_query' => array(
    array(
        'key' => 'seu_meta_key',
        'value' => array( 'data_atual', 'data_termino' ),
        'compare' => 'BETWEEN',
        'type' => 'NUMERIC'
    )
);

You can also try using type as DATE, but to do this you will also have to use a date format as I said.

For more details see documentation of the use of meta_query in WP_Query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.