WordPress.org

Support

Support » Plugins and Hacks » meta_query of unix timestamp to custom field

meta_query of unix timestamp to custom field

  • Topher

    @topher1kenobe

    I have a custom field that holds a unix timestamp, and I’ve verified that the data in it is proper.

    In my code I’m trying to compare that timestamp against $now, where $now is php’s time(); $now is also holding valid data.

    I wanted to get posts where $now is less than my field. I’m using this:

    $args = array(
    ‘post_type’ => ‘closings’
    ,’posts_per_page’ => 1000
    ,’meta_query’ => array(
    array(
    ‘key’ => ‘date_time’,
    ‘value’ => $now,
    ‘compare’ => ‘<=’,
    ‘type’ => ‘NUMERIC’
    )
    )
    );

    I’m not getting any results out of it. I tried with DATETIME instead of NUMERIC, and I tried reversing my comparison operator.

    The docs aren’t clear how the comparison values are ordered. For my code above is it saying ‘date_time <= $now’ or ‘$now <= date_time’ ?

    Am I using the wrong type?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Topher

    @topher1kenobe

    I think I figured out part of it, but not how to fix it. I forgot that date_time is part of an array that looks like this:

    Array
    (
        [date_value] => Array
            (
                [0] => 1307736000
            )
    
        [_edit_lock] => Array
            (
                [0] => 1307719655:1
            )
    
        [_edit_last] => Array
            (
                [0] => 1
            )
    
    )

    However, this still doesn’t work:

    Array
    (
        [post_type] => closings
        [posts_per_page] => 1000
        [meta_query] => Array
            (
                [0] => Array
                    (
                        [key] => date_time[0]
                        [value] => 1307734041
                        [compare] => >=
                        [type] => NUMERIC
                    )
    
            )
    
    )
    Topher

    @topher1kenobe

    Score, I *was* something simple. My args were looking for date_time and my array holds date_value.

    This works:

    $args = array(
            'post_type'      => 'closings'
            ,'posts_per_page'   => 1000
            ,'meta_query' => array(
                array(
                    'key' => 'date_value',
                    'value' => $now,
                    'compare' => '>=',
                    'type' => 'NUMERIC'
                )
            )
        );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘meta_query of unix timestamp to custom field’ is closed to new replies.
Skip to toolbar