Treehouse: Grow your CSS skills. Land your dream job.

WordPress: Use query_posts to order_by tag alphabetically

  • # January 4, 2011 at 4:06 pm

    I’ve been googling around for a while and going through the WP codex to try to find a way to do this. I can use ‘order_by=tag’ within query_posts but it doesn’t order the posts alphabetically by tag name. I’ve tried combinations using ‘order=ASC’ and ‘order=DESC’ but nothing seems to work. Does anyone know if there’s even support for this in WordPress using query_posts? Would I have to try a different approach and maybe use an array?

    Many thanks in advance, I really appreciate it.

    # January 4, 2011 at 4:32 pm

    Just checked the codex, and it doesn’t look like the tag ordering is possible outright. Probably because each post could potentially have hundreds of tags. Which tag would it choose to organise with?

    Is this related to your previous discussion about tabs and sorting? What is the end result of ordering by tags?

    # January 5, 2011 at 11:58 am

    Hey clokey thanks for the reply again, really appreciate your input. The filtering blocks worked great and I was able to categorize the posts into tabs by their tag-name. While the filtering blocks allow me to filter certain posts it doesn’t allow me to display all of the posts in a specific order (in this case alphabetically). I have 5 options, the first is ‘All’ the second is ‘Type’ and the last 3 are categories. The ‘All’ and the categories work fine using the filtering blocks, but the ‘Type’ option needs to list all of the posts in order by tag-name alphabetically.

    I understand what you’re saying about there being multiple tags and not knowing which one to use to order by, I was hoping it would just take the first one and use that, but unfortunately not. I thought that order_by=tag was ordering the posts by tag ID, but after further investigation it was just coincidence!

    I’m by no means an expert in PHP but do you know whether it’s actually possible to achieve this?

    # January 5, 2011 at 2:01 pm

    I think you’ll have to move back to the drawing board on this one…

    Ordering by tag’s could be done using:

     < ?php $tag = wp_tag_cloud('format=array' );?>

    Which would create an array of all tags used. Then loop through each tag and run a loop of posts containing that tag. It would be messy… and any post with multiple tags will appear in the list for each tag it contains.

    You might have to resort to an AJAX call to load them…

    # January 5, 2011 at 4:24 pm

    Yeh I’m gonna have to go back and rethink this one, might be more trouble than it’s worth for me to organize posts this way. Thanks again for the help, really appreciate it.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.