I have a custom field called "value_date" where add a date e.g yyyy/mm/dd
The idea is sort the upcoming dates first in the loop, is possible?
I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.
Functions.php
add_action('pre_get_posts','search_filter');
function search_filter($query){
if ( !is_admin() && $query->
is_tax( 'event', 'date' ) ) {
$today = date( 'Y-m-d' );
$query->set('post_status', 'publish');
$query->set('meta_value', $today);
$query->set('meta_key', 'value_date');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
}
}
I have a custom field called "value_date" where add a date e.g yyyy/mm/dd
The idea is sort the upcoming dates first in the loop, is possible?
I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.
Functions.php
add_action('pre_get_posts','search_filter');
function search_filter($query){
if ( !is_admin() && $query->
is_tax( 'event', 'date' ) ) {
$today = date( 'Y-m-d' );
$query->set('post_status', 'publish');
$query->set('meta_value', $today);
$query->set('meta_key', 'value_date');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
}
}
Share
Improve this question
edited May 16, 2017 at 17:52
Max
asked Apr 19, 2017 at 16:22
MaxMax
536 bronze badges
1
|
1 Answer
Reset to default 1Yes, You can call action "pre_get_posts".
add_action('pre_get_posts','search_filter');
You can add parameter :
$args = array(
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
You can use above parameter e.g. :
function search_filter($query){
if ( !is_admin() && $query->is_main_query() ) {
$query->set('post_status', 'publish');
$query->set('meta_key', 'event_date');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
}
}
pre_get_posts
questions, however- your date format is incorrect and will need to be changed to yyyy/mm/dd to sort correctly. – Milo Commented Apr 19, 2017 at 17:09