Answers for "pre query change order"

0

pre query change order

add_action( 'pre_get_posts', 'cd_sort_staff' );
function cd_sort_staff( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
        if ( $query->is_tax() || $query->is_post_type_archive('staff') ) {
            $query->set('orderby', 'meta_value_num');  
            $query->set('meta_key', 'sort_order');  
            $query->set('order', 'ASC'); 
        }       
    }
}
Posted by: Guest on October-14-2021

Browse Popular Code Answers by Language