scooby scooby - 1 year ago 70
PHP Question

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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download