Posts query including meta and greater than date

I’m struggling to get a working solution with this wp_query. I currently have some custom settings which are assigned to posts, one is whether or not the post is ‘featured’ and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the ‘featured’:

WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date');

The end date is set in the wp_postmeta table where meta_key is ‘the_date’ and the meta_values look like this ’05/16/2013 05:24′. I would like to edit the above query where if ‘the_date’ has been set posts are only included if the ‘the_date’ is greater that todays date and time.

Read More

Here is my failed attempt:

WP_Query(
   'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
   .date('d/m/Y H:i')
   .'&showposts=4&orderby=post_date&orderby=the_date'
);

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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);
    
  3. 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);