2

I would like to use something similar to the following date_query but I would like to use some custom meta fields instead of the existing post columns. For example instead of post_date_gmt I would like to query the meta key date which I have created.

In other words, I would like to be able to filter my custom_post_types using some meta fields e.g. starting_date or end_date in view them in the administration interface. I have already intercept the main WP_Query but I am not sure how I can filter a meta field instead of a post column.

Can I do that or is it better to create a meta query?

// Any posts made over a year ago
// but modified in the past month
$some_posts = new WP_Query( array(
    'date_query' => array(
        array(
            'column' => 'post_date_gmt',
            'before' => '1 year ago',
        ),
        array(
            'column' => 'post_modified_gmt',
            'after'  => '1 month ago',
        )
    ),
    'posts_per_page' => -1,
) );
4
  • You need to use a meta_query for custom fields, and your custom field dates must be stored in the format of yyyy-mm-dd h:m:s May 7, 2015 at 9:44
  • Can I ask why you're using a custom meta to store the post date, rather than just using the post date?
    – Tom J Nowell
    May 7, 2015 at 9:45
  • I don't use a custom meta to store the post date, I have a new custom post e.g. Taxi reservation and I use a custom meta to store the pickup date. I would like to be able to filter the posts in admin interface by meta_pickup_date and meta_return_date.
    – glarkou
    May 7, 2015 at 9:50
  • @PieterGoosen can you provide a small example as well to mark your answers ? Additionally, can I have a date_query inside a meta query?
    – glarkou
    May 7, 2015 at 9:53

1 Answer 1

4

You need to use a meta_query to query custom fields. If you are going to store date and time in custom fields for the purpose of ordering or comparisons, you will need to store it in the format of yyyy-mm-dd h:m:s or as a unix timestamp.

If you have two custom fields start and end and you need to get posts between a cetain start and end date, you can try the following

$args = [
    'meta_query' => [
        [
            'key' => 'start',
            'value' => '2014-05-14'
            'compare' => '>=',
            'type' => 'DATE'
        ],
        [
            'key' => 'end',
            'value' => '2015-05-07',
            'compare' => '<=',
            'type' => 'DATE'
        ],
    ],
];

$q = new WP_Query( $args );

FEW NOTES:

  • The above requires PHP 5.4+ as it uses the new short array syntax

  • The above is totally untested and might be buggy. I'm posting froma mobile device :-)

  • Read the links I have supplied above

EDIT

For some funny reason unknown, I've missed the last part of your comment.

Additionally, can I have a date_query inside a meta query

The answer is no, but you can have a date and meta query within the same query, like this

$args = [
    'date_query' => [[ /* Your date related stuff */]],
    'meta_query' => [[ /* Your custom field related stuff */ ]],
];
$q = new WP_Query( $args );
3
  • Seriously, 1000 thanks. I will test the code and I will update your answer if required. Thanks once again
    – glarkou
    May 7, 2015 at 10:50
  • No problem, feel free to make the necessary changes if needed. Enjoy :-) May 7, 2015 at 10:52
  • You can have it also in one meta query "item" by using between : $meta_query[] = ['key' => 'my_meta_key','value' => ['2020-05-07','2020-11-07'],'type' => 'DATETIME','compare' => 'BETWEEN']; Nov 1, 2020 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.