WordPress.org

Ready to get started?Download WordPress

Forums

Sorting posts on taxonomy page alphabetically like an index (3 posts)

  1. jageo
    Member
    Posted 3 years ago #

    <?php
    global $wp_query;
    query_posts(
    	array_merge(
    		array(
    		'order' => 'ASC',
    		'orderby'=>'title',
    		'posts_per_page' => 20,
    		),
    		$wp_query->query
    	)
    );
    ?>

    I'm putting this here because it gave me so much grief to figure out that I'm sure there has to be some other noob looking to do the same thing. Plus I'll be able to find it again next time I need it.
    I have a newspaper website, with a category 'Obituaries', and custom taxonomy "obit year" (because obits are not always submitted in a timely manner, I can't make use of the post-date)
    I made a category-obituaries template so when someone views Obituaries they see the 5 most recent postings and made a list in the sidebar of the taxonomy terms (ie. 2010, 2009, 2008) using a wp_list_categories($args).
    Clicking those terms brings you to the taxonomy-obityear.php template where I needed to sort alphabetically. The above code goes in that template just after the "if have_posts()"
    The key was in the Codex under "Preserving the Original Query. I read so many posts on how to query without any mention of this. I couldn't figure out how to keep the original loop but customize the presentation.
    It's so simple....once you know the answer ;)

  2. vtxyzzy
    Member
    Posted 3 years ago #

    One thing to note: If you want to override something from the original query, you must place your argument array after the $wp_query->query array.

    <?php
    global $wp_query;
    query_posts(
    	array_merge(
    		$wp_query->query,
    		array(
    		'order' => 'ASC',
    		'orderby'=>'title',
    		'posts_per_page' => 20,
    		)
    	)
    );
    ?>

    This is because array_merge will keep the last value it finds for any given key.

  3. jageo
    Member
    Posted 3 years ago #

    That seems the safest way to order it then.
    One is unlikely to add an argument that they wish to be overridden.
    Thanks for the addition.

Topic Closed

This topic has been closed to new replies.

About this Topic