我需要更改分类中的帖子。php页面。我有一个meta\\u键,我想将其与meta值进行比较。但目前我的代码没有返回任何内容。我肯定我错过了什么。请让我知道我做错了什么!
add_action(\'pre_get_posts\', \'add_event_date_criteria\');
function add_event_date_criteria(&$query)
{
// We only want to filter on "public" pages
// You can add other selections, like here:
// - Only on a term page for our custom taxonomy
if (!is_admin() &&
is_tax(\'event-tag\') || is_tax(\'event-category\')) {
$query->set(\'meta_key\', \'start_time\');
$query->set(\'meta_compare\', \'>=\');
$query->set(\'meta_value\', time());
$query->set(\'meta_key\', \'start_time\');
$query->set(\'orderby\', \'meta_value_num\');
$query->set(\'order\', \'ASC\');
}
}
最合适的回答,由SO网友:Bainternet 整理而成
尝试使用meta\\u查询参数:
add_action(\'pre_get_posts\', \'add_event_date_criteria\');
function add_event_date_criteria(&$query)
{
// We only want to filter on "public" pages
// You can add other selections, like here:
// - Only on a term page for our custom taxonomy
if (!is_admin() &&
is_tax(\'event-tag\') || is_tax(\'event-category\')) {
$time = time();
$meta = array(
array(
\'key\' => \'start_time\',
\'value\' => $time,
\'compare\' => \'>=\'
)
);
$query->set(\'meta_query\',$meta );
$query->set(\'meta_key\', \'start_time\');
$query->set(\'orderby\', \'meta_value_num\');
$query->set(\'order\', \'ASC\');
}
}
您还需要确保您的元字段
start_time
以Unix类历元time()函数之后的秒数来度量和保存。