Product order based on taxonomy and/or ACF field

I have some WooCommerce products with a brand taxonomy attached to each. I have created an ACF number field: Order by and assigned to both product post type and brand taxonomy.

I would like to alter the order of the products queried on for e.g. a category page (where there are products from different brands), based on the ACF Order by field’s value as follows:

Check the attached brand taxonomy’s Order by ACF value, and if it is not empty or 0, order by the given value ASC;
But if a product has it’s own Order ACF value filled (not empty or 0), than it should order by that instead of what is given by the attached brand taxonomy (also ASC);
Also, there should be a secondary order critera: post_title ASC.

I have no idea how to write a query for this, but a possible solution that came to my mind was querying the products based on the main criteria (for e.g. category), and than altering/ sorting the received array – but it would be awesome if someone could provide help on how to write the WP or MySQL query for this, without any additional processing. Thank you in advance.

Read more here:: Product order based on taxonomy and/or ACF field

Leave a Reply

Your email address will not be published. Required fields are marked *