I have a blog with movie critics and I'm using many taxonomies to add some informations about the movies, like director, actors or it's year. Using taxonomies is really useful to display all movies made by the same director (example here).

My only concern is with the release year taxonomy. I'd like to keep this information as a taxonomy, since I can, for instance, have a list of all movies released 10 years ago. I'd like to use this information to sort all other taxonomy archives, since it's almost always more interesting than other sort options. For now, I'm using the name of posts, but it can lead to strange behavior, like here, for the Iron Man saga where movies are not in the right order. If I could sort this list with year of release, it would be much more interesting.

I understood WordPress doesn't want to add taxonomy orderby in queries, and I'd like tu duplicate this taxonomy and add a release date as custom field, since I can sort using a custom field. I made lot of searches and all I could find was the other way (converting a custom field to a taxonomy).

Could you help me adding the date of every posts to a custom field ? I don't know if I should do it with a SQL query (if so, could you tell me what query I should use ?) or with PHP… Or even if I should do something entirely different to resolve my problem…

Thanks for your help !

  • It looks like they're in order of when the you created it in WordPress, with the newest first. Maybe you can alter your loop to arrange by release date instead. Here's a little more info. – NW Tech Apr 29 '13 at 17:36
  • Thanks but I can't use the native WordPress feature for that, since taxonomy is not a correct parameter for wp_query… Maybe I misunderstood what you told me, though. – nicolinux Apr 29 '13 at 18:05
  • How many posts do you need to update? If it's a few, I'd do it by hand. If it's many, I'd create a temporary wp_query to loop through them all, and for each grab it's release date term. Then update_post_meta with that year. – vancoder Apr 29 '13 at 18:07
  • Something between 500 and 600 posts to update, so I don't want to do by hand… :-) Could you help me for the method you're describing, or at least point me to the right direction ? – nicolinux Apr 29 '13 at 18:13

You can order posts by custom taxonomy terms by adding a posts_clauses filter, see below answer:


Make sure that on functions.php you add both that function AND the call to add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );, otherwise it won't work.

Assuming annee is the custom taxonomy, this should then work:

query_posts($query_string . '&posts_per_page=-1&orderby=annee&order=ASC');

  • I know this is not what you asked, but rather what you didn't ask because you thought it was not possible. Hope it helps! – guidod Apr 29 '13 at 20:32
  • OK, I will try to understand it, at least. For now, it's like chinese to me, I'm really bad at PHP and SQL, I'm afraid. :-s But thanks anyway ! – nicolinux Apr 29 '13 at 20:44
  • I added the code you pointed in functions.php, but I don't know how to use it in my theme. I want to sort taxonomy archives using taxonomy "annee" and I tried this, without success : <?php query_posts($query_string . '&posts_per_page=-1&orderby=(annee)&order=ASC'); ?>What should I put after orderby= ? – nicolinux Apr 30 '13 at 7:12
  • Hi Nicolinux! Assuming annee is the custom taxonomy, this should work (no brackets wrapping the taxonomy name): <?php query_posts($query_string . '&posts_per_page=-1&orderby=annee&order=ASC'); ?> Make sure that on functions.php you add both the function AND the call to add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );, otherwise it won't work. Hope it helps! – guidod Apr 30 '13 at 12:20
  • Arf, I was searching for something more complex… :-) So it works, or not : with the correct taxonomy, I have an empty page. I have tried all my registered taxonomies and none worked. I checked the code in functions.php and everything seems fine. I don't know what's going on… – nicolinux Apr 30 '13 at 15:06

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.