Posts query including meta and greater than date Posts query including meta and greater than date wordpress wordpress

Posts query including meta and greater than date


I had to do something very similar recently and ended up needing to use the meta_query property instead. You'll want to do something like this:

$today = date('Ymd');$args = array(    'post_type' => 'post',    'posts_per_page' => '4',    'meta_key' => 'the_date',    'meta_query' => array(        array(            'key' => 'skyali_feature'        ),        array(            'key' => 'the_date',            'value' => $today,            'compare' => '>='        )    ),    'orderby' => 'meta_value_num',    'order' => 'ASC');$your_custom_query = new WP_Query($args);

A few notes...

  • I only needed to filter by date in my example, but it looks like you'll need to do date/time in yours. (You can just adjust the first line for the $today variable using the format you wish).

  • Use posts_per_page instead of showposts. showposts is deprecated.

  • Notice that I have included the meta_key twice (once at the top level of the array and once as an element in the meta_query array. There's a known bug where you can't sort your results by the key if you don't include it this way. I fought that one for a while too!

Hope this helps, have fun!

[edit] Forgot to add your skyali_feature key back into the array.


for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:

    $today = date('Ymd');    $args = array(        'post_type' => 'post',                    'meta_key' => 'end-date',        'meta_query' => array(            array(                'key' => 'end-date'            ),            array(                'key' => 'end-date',                'value' => $today,                'compare' => '>='            )        ),        'orderby' => 'meta_value',        'order' => 'ASC'    );   $your_custom_query = new WP_Query($args);


for people using Custom metadata manager you'll find that a datepicker field is stored as timestamp.

So in a similar case the above example isn't working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it'll do the job:

    $yesterday = strtotime('yesterday 23:59:59');    $args = array(        'post_type' => 'post',                    'meta_key' => 'end-date',        'meta_query' => array(            array(                'key' => 'end-date'            ),            array(                'key' => 'end-date',                'value' => $yesterday,                'compare' => '>='           )        ),        'orderby' => 'meta_value',        'order' => 'ASC'    );    $your_custom_query = new WP_Query($args);

If you also want to take account of the timezone setting for the blog use current_time() as in the following example:

    $now = current_time('timestamp');    $yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1);