Hi,
I am searching a custom post type named seed_companies and would like to display the results in alphabetical order (ASC). I tried adding this code to my functions.php but it didn’t work.
function custom_order_search_results($query) {
if (!is_admin() && $query->is_main_query() && $query->is_search() && $query->get('post_type') === 'seed_companies') {
$query->set('order', 'ASC');
$query->set('orderby', 'title');
}
}
add_action('pre_get_posts', 'custom_order_search_results');
Is there a better way to do this?
Thank you!