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' );
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' );
Share
Improve this question
asked Jun 20, 2019 at 7:03
Gregory SchultzGregory Schultz
6236 silver badges31 bronze badges
3
- What date format do you use for your custom field? – Krzysiek Dróżdż Commented Jun 20, 2019 at 7:19
- Why not just make a custom query for this? You can compare that way using similar code to what you have before. – rudtek Commented Jun 20, 2019 at 17:31
- @rudtek wordpress.stackexchange/questions/341174/… – Gregory Schultz Commented Jun 22, 2019 at 7:23
1 Answer
Reset to default 1What 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385090a4625403.html
评论列表(0条)