• Resolved ojasya

    (@ojasya)


    I am try to sort all archive pages bu menu_order using the following code

    add_action( 'pre_get_posts', 'my_change_sort_order'); 
        function my_change_sort_order($query){
            if(is_archive()):
             //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
               //Set the order ASC or DESC
               $query->set( 'order', 'ASC' );
               //Set the orderby
               $query->set( 'orderby', 'menu_order' );
            endif;    
        };

    Normally it works fine on all pages
    but ..
    on search pages that default sorting doesn’t work..

    Can you please help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author ILLID

    (@mihail-barinov)

    Hello,

    Can you please explain me more carefully what exactly you want to achieve?

    You want to sort by menu order results that shows product categories and tags archive pages?
    Current search plugin works only with pages that are archives of WooCommerce products taxonomies. Its doesn’t works with standard pages.

    Regards

    Thread Starter ojasya

    (@ojasya)

    Actually we want to display products one particular category at top always(i.e sorted by one particular category)
    but since they was not query to sort by particular category ..

    SO I assigned the value -999 to menu_order all the products of this particular category

    And used the above code to sort them by that category .It works fine on shop page and other archive pages , or pages that come up after we select any woocommerce filters.

    But then there is search page through your plugin..

    FOR Eg: when we search for eg: “marh” , 100 products result are listed .. out of which 40 are from that particular category(menu order is -999) .. so those 40 producs should come up first in the results

    Plugin Author ILLID

    (@mihail-barinov)

    Ok, looks like I understand what you are talking about.

    I have one solution for you how to show products with specific product category in the top of search results.

    Please add code below to your theme functions.php file:

    add_filter( 'aws_search_results_products', 'aws_search_results_products' );
    
    function aws_search_results_products( $products ) {
    
        usort($products, function ($item1, $item2) {
    
            $priority_term_id = 15;
    
            $a = aws_get_terms_array( $item1 );
            $b = aws_get_terms_array( $item2 );
    
            if ( in_array( $priority_term_id, $a ) && ! in_array( $priority_term_id, $b ) ) {
                return -1;
            } else if ( ! in_array( $priority_term_id, $a ) && in_array( $priority_term_id, $b ) ) {
                return 1;
            } else {
                return 0;
            }
            
    
        });
    
        return $products;
    
    }
    
    function aws_get_terms_array( $product ) {
    
        $data = $product['post_data'];
        $term_array = array();
    
        $terms = get_the_terms( $data->ID, 'product_cat' );
    
        if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
    
            $term_array = array();
    
            foreach( $terms as $term ) {
                $term_array[] = $term->term_id;
            }
    
        }
    
        return $term_array;
    
    }

    In this code you only need to change id 15

    $priority_term_id = 15;

    to ID of category that must be displayed in the top of results.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘sorting by menu order doesn’t work’ is closed to new replies.