Home › Forums › Front-end Issues › sort posts based on meta key in a taxonomy archive page › Reply To: sort posts based on meta key in a taxonomy archive page
function change_order_for_events( $query ) {
//only show future events and events in the last 24hours
$yesterday = date('Ymd');
if ( $query->is_main_query() && (is_tax('dt_portfolio_category') || is_post_type_archive('dt_portfolio')) ) {
$query->set( 'meta_key', 'sub_seminars_0_start_date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
//Get events after 24 hours ago
$query->set( 'meta_value', $yesterday );
$query->set( 'meta_compare', '>' );
//Get events before now
//$query->set( 'meta_value', current_time('timestamp') );
//$query->set( 'meta_compare', '<' );
}
}
add_action( 'pre_get_posts', 'change_order_for_events' );
this code in function.php will sort your posts based on a specific meta key value.
sub_seminars_0_start_date
I have created a custom taxonomy Seminar Venues
but the above code won’t work for that even after adjustin the parameters.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.