我正在寻找一种将字符串与当前时间进行比较的方法。我发现了这个问题“pre_get_posts filter using numeric meta_query comparison (from dates)“我对如何让它发挥作用感到困惑。
$expire = get_field(\'status_time_duration\') + get_the_time(\'U\'); // UNIX time that gets the publish date plus an additional time in seconds
基本上,我的查询是在字符串$expire
小于当前时间。我的当前代码:
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\' );