pre get posts changing the query
-
16-10-2019 - |
Pergunta
i need to change the posts in taxonomy.php page. I have a meta_key which i want to compare to meta value. But currently my code is not returning anything. I am sure i am missing something. Please let me know what i am doing wrong!
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');
}
}
Solução
Try using meta_query parameter :
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');
}
}
also you need to make sure your meta field start_time
is measured and saved in the number of seconds since the Unix Epoch like time() function.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange