0

I'm trying to run a query that only shows posts if it's meta value has a date string that is LESS than the current date's string. An example is below;

<?php
    $current = date('Ymd'); // Eg: today would be 20131011
    $date = get_post_meta($post->ID, 'event_date', true); // (Eg; 19690816)
    $args (
        'post_type' => 'event',
        'posts_per_page' => -1,
        'meta_query' => array(
            'key'     => 'event_date',
            'value'   => $current,
            'compare' => '<',
            'type' => 'numeric'
        ) 
    );
?>

But it doesn't seem to be working.

The $date value is defined by a meta box and returns a string in the same format as the $current value above so it's basically just comparing 2 numeric values against each other.

Anyone have any ideas? I've tried it the opposite way, using the greater than symbol and that doesn't want to work either.

I've quadruple checked the post type name, the values, etc and all are correct.

0

Your code looks valid for me. The only thing you can try is 'NUMERIC' instead of 'numeric' and to cast $current to int as date() will return a string.

$current = (int)date('Ymd');

To debug this further you can place this directly under your code:

global $wpdb;
var_dump( $wpdb->last_query );

You'll then have the complete query and can paste it into a sql-shell (phpMyAdmin or whatever you prefer) to see what's wrong with it.

If this doesn't solve your question feel free to edit your question with the complete SQL query and I'll have a look at it.

0

A meta_query is an array of arrays. You only have an array (and a syntax error). You need this:

$current = date('Ymd'); // Eg: today would be 20131011
$date = get_post_meta($post->ID, 'event_date', true); // (Eg; 19690816)
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query' => array(          // an array
      array(                        // of arrays
        'key'     => 'event_date',
        'value'   => $current,
        'compare' => '<',
        'type' => 'numeric',
      )
    ) 
);
$q = new WP_Query($args);
var_dump($q->request);

I doubt that 'type' => 'numeric', is necessary though.

Notice the last line... very useful for debugging these things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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