WordPress.org

Ready to get started?Download WordPress

Forums

meta_query of unix timestamp to custom field (3 posts)

  1. Topher
    Member
    Posted 3 years ago #

    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?

  2. Topher
    Member
    Posted 3 years ago #

    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
                    )
    
            )
    
    )
  3. Topher
    Member
    Posted 3 years ago #

    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'
                )
            )
        );

Topic Closed

This topic has been closed to new replies.

About this Topic