How to use the ID number as the default sort field for products under Woocommerce classification

Source: Internet
Author: User
Tags woocommerce plugin wordpress version

Woocommerce plugin for WordPress system used in the development of online store system for a customer

WordPress Version: 3.8
Woocommerce version: 2.0.20 If no collation is specified (the specified field), theWoocommerce is used by default "Post_title "to sort of, but I was inwhen you call a product under classification, use theID field to customize the collation, but it is not actually sorted according to a custom rule, but instead uses theORDER by Menu_order,post_title ASC "to sort(inThe ID is not sorted in the Product_category method) in theWoocommerceA custom rule was found in the plugin help, but it still didn't work. 
 search on Google for half a day and did not find a solution, so instead to see the source solution, so in \wp-content\plugins\woocommerce\classes\ class-wc-shortcodes.php find the definition of the Product_category method and output sort information Foreground generated SQL statement found in foreground the output collation is not the same as my custom rules, and then continue to view the get_ in \wp-content\plugins\woocommerce\classes\class-wc-query.php Catalog_ordering_args Method OriginalThe Get_catalog_ordering_args method is also processed when sorting, so the method that does not conform to the above rule will reset the collation, fortunately the method provides a "Woocommerce_get_catalog_ordering_args "Filterand redefine it in the Funciton.Woocommerce_get_catalog_ordering_args "and return my custom collation finally sorted by ID, add the following code to the function of the topic
Add_filter (' Woocommerce_get_catalog_ordering_args ', ' Htl_woocommerce_get_catalog_ordering_args ' ); /* * * HTL Add 2014-10-28 * Product collation under custom category * This rule resets the sort field in the Product_category method * @param  [Type] $args [sort Array] * @return [type]       */function$args  )     {$args /c10>[' by "]  = ' ID ';       // echo "<br>";     Printer_r ($args);    return $args ;}

here is the SQL statement printed on the foreground page 
This solves the Woocommerce default use ID to sort products under classification, but one problem is if I follow the "Menu_order "in descending order and then in descending order by ID, this sort is done by multiple fields .in Wordpress->The Wp_query class does not seem to be sorted by multiple fields (Woocommerce finally calls theWp_query to get the data),if more than one sort field appears,wp_query Default Use "post_date "to sortLater through Solagirl know that the wp4.0 later version has supported multiple sort fields, but the following version of 4.0 does not work, it seems that only by similar $wpdb this way to write their own SQL to sort the reference:            WordPress Wp_query method            4 ways to customize WordPress queries            A more powerful ORDER by in WordPress 4.0            Woocommerce Docs Sorting Products by Custom Meta fields           From for notes (Wiz)

How to use the ID number as the default sort field for products under Woocommerce classification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.