WooCommerce Product Search Result Ordering

Any idea how to modify this script i wrote below so it ONLY applies to woocommerce product pages?

/**
* Sort product search results by menu order
*/

add_action('pre_get_posts','sortby_menuorder_query');

function sortby_menuorder_query($query) {
    if ( isset($_REQUEST['s']) ) {
        $query->set('orderby', 'menu_order');
        $query->set('order', 'ASC');
    }
}

I thought it would help make it so the products would appear in the correct order based on Menu appearance set in the product backend, and it totally does. But then it also applies that same menu order logic to every single page, post, and CPT on the entire website. So it fixes my product search, and breaks my standard search.

Hi Jesse,

Thank you for writing in, while that is outside the scope of support, I could point you in the right direction with the understanding that it would ultimately be your responsibility to take it from here. Please follow the link below for available Woocommerce Conditional Tags.

Thank you for understanding.

The conditionals provided in that link aren’t much guidance.

There’s no simple recommend you can offer for how to add a conditional like [if “post_type=product”] somewhere in there. I’m pretty bad at custom scripting.

Hello Jesse,

Please be advised that custom coding is beyond the scope of our support already. We are unable to provide support for customizations under our Support Policy.

Note: If you are unfamiliar with code and resolving potential conflicts, you may select our One or Care service for further assistance.

Thank you for your understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.