0

Yesterday i stuck in the middle of code that how could i retrieve product belong to specific custom taxonomy ordered by other taxonomy.

Table wp_posts:

 id    |  post_title
----------------------
 2     |    Galaxy S4
 4     |    Iphone i5
 6     |    Nokia lumia 535

 Table wp_term_realtionship:-

 object_id |  term_taxonomy_id
 -----------------------------
      2    |      12
      4    |      12
      6    |      12
      2    |      15
      4    |      16
      6    |      17

  Table wp_terms:-

   term_id   |   name
   -------------------
     12      |   Smartphone
     15      |   Samsung
     16      |   Apple
     17      |   Nokia

Now Smartphone is from product_cat and brands (samsung,apple,nokia) from product_brand taxonomy. Now if user goes to smartphone category and order by brand (A to Z) what query should i write. The code which i tried so far:-

Code:
 SELECT * FROM wp_posts INNER JOIN wp_term_relationships ON wp_posts.id=wp_term_relationships.object_id INNER JOIN wp_terms wp_terms.term_id=wp_term_relationships.term_taxonomy_id WHERE wp_terms.term_id=18 ORDER BY wp_terms.name DESC;  

Now above code result product of smartphone category but not in order by product brand. Please help me to rectify above code. Thanks.

2
  • 1
    This should be aan edit to your previous question Sep 12 '15 at 10:14
  • @PieterGoosen yeah may be. But i cant find a solution.
    – Jason
    Sep 12 '15 at 10:16
1

WordPress is notoriously against ordering by terms. Of course it's precisely what people need to accomplish in practice. And precisely a mess with a very clunky database representation of them (which is planned to be slowly improved over several releases to be fair).

In a nutshell the issues with your query are:

  • it's preferable to work with WP_Query, even if you are considerably customizing SQL
  • you are missing the whole third wp_term_taxonomy table :)

While I don't think there is any truly canonical way to do it, the most reliable approach I had seen and used is covered in Sortable Taxonomy Columns post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.