0

I'm looking for a way to compare a string to the current time. I found this question "pre_get_posts filter using numeric meta_query comparison (from dates)" and I'm confused as to get it to work.

$expire = get_field('status_time_duration') + get_the_time('U');  // UNIX time that gets the publish date plus an additional time in seconds

Basically, my query is to get posts if the string $expire is less than the current time.

My current code:

function alerts_opby( $qvars ){ //function to call up the query
$qvars[] = 'opby_alerts';
return $qvars;
}
add_filter( 'query_vars', 'alerts_opby' );

function opby_query( $query ) { //pre_get_posts functions
if (isset( $query->$qvars['opby_alerts'] )) {
    $expire = get_field('status_time_duration') + get_the_time('U');
    $current = date( 'U', current_time( 'timestamp', 0 ) );
    $query->set('tax_query', array(array('taxonomy' => 'post_format','field' => 'slug','terms' => array( 'post-format-status' ),'operator'=> 'IN'),
),'meta_query', [
    'relation' => 'OR',
    [
     'key'     => 'status_time_duration',
     'value'   => $expire,
     'compare' => '<=',
     'type'    => 'NUMERIC',
    ],
    [
     'key'     => 'status_time_duration',
     'compare' => 'NOT EXISTS',
    ],
],
    $query->set('posts_per_page', -1));
}
return $query;
}
add_action( 'pre_get_posts', 'opby_query' );
1

What you're trying isn't possible with WP_Query.

The reason is that because you have only stored the duration, it's not possible to tell whether a post has expired until you know the publication date and the duration time and add them together. That's what this does:

$expire = get_field( 'status_time_duration' ) + get_the_time( 'U' ); 

That code relies in you already having the status duration and time for the post. When pre_get_posts is run, the posts have not even been queried, as the name suggests, which means you can't perform that addition.

It might be possible to do the necessary addition and comparison in a raw SQL query, but WP_Query does not provide the capability to do this sort of query.

For these reasons, storing the expiration time as a duration is a poor choice if you intend to query it this way. It would be far easier to store the absolute date and time that the post was set to expire, then a simple meta query could be used to compare that date to the current date:

'meta_query', [
    'relation' => 'OR',
    [
        'key'     => 'status_time_duration',
        'value'   => date( 'U' ),
        'compare' => '<=',
        'type'    => 'NUMERIC',
    ],
    [
        'key'     => 'status_time_duration',
        'compare' => 'NOT EXISTS',
    ],
],

Note that the above code is only an example for if you changed how you stored the expiration time, and won't work with your current setup.

  • 1
    Thanks for the help, it really made me better understand what to do. I went ahead and created a custom field that gets the publish date and adds additional time. It's saved in UNIX time. Is there to see what the pre_get_posts gets because I've done a lot and nothing works. $query->set('meta_query', array(array('key' => 'breaking_news_status','value' => date( 'U' ),'compare' => '>','type' => 'DATE'))); – Gregory Schultz Jun 21 at 8:54
  • If the value is in unix time, the type is just numeric. Are you trying to query posts that have expired, or not expired? If the current time is greater the expiration time then that means it's expired, which is what you're querying with that code. – Jacob Peattie Jun 21 at 9:03
  • Posts that are not expired. – Gregory Schultz Jun 21 at 9:07
  • Ok, sorry, I got that muddled in my head. > is correct. Definitely try changing the type to NUMERIC though. Also make sure you’ve republished your posts after adding the field, and maybe double check the database to see if the value is there and what you expect. – Jacob Peattie Jun 21 at 9:14
  • I'm going to try something else cause I can't get it to work. Code I have now 'meta_query', array( array( // restrict posts based on meta values 'key' => 'breaking_news_status', // which meta to query 'value' => date("U"), // value for comparison 'compare' => '<', // method of comparison 'type' => 'NUMERIC'))) ; shows all posts even if the current time is greater than the value set in the breaking_news_status. – Gregory Schultz Jun 21 at 21:33

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.