简体   繁体   中英

WP Woocommerce - how to change the ordering of the search products

how can I change the order of products in the general site search? I need to order by product name.

where is the class that performs the search?

tks.

Try the below code in your functions.php file:

add_filter( 'posts_orderby', 'sort_custom' );
function sort_custom( $orderby ){
    global $wpdb;

    if( !is_admin() && is_search() ) {
      $orderby =  $wpdb->prefix . "posts.post_title ASC";
    }    

    return $orderby;
}

One thing to note. If you are using a search plugin like Relevanssi you will need to turn it off for this code to work. You may be able to mess with the priority of the call to get it to work but I didn't look into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM