• Resolved dorich

    (@dorich)


    I have a taxonomy template that is returning the correct posts (actually a custom post type) but I want to alter the order in which the posts are displayed. However, nothing I’ve tried will change the order of the posts from chronological.
    I’ve assumed that since the posts have already been returned then I should use ‘query_posts’ to alter the order of the posts. Accordingly, [based on the codex][1], I added the following before the loop on the template:

    <?php
    global $query_string;
    query_posts( $query_string . ‘&order=>ASC’);
    ?>

    Changing “ASC” to “DESC” produces no changes in the listing.

    NOTE: The end result I want will be more complicated than this example but for the moment I can’t even get a simple sort like above to work.

    I’ve also tried using this approach

    <?php $sectionarg = array(
    ‘order’ => ‘ASC’,
    );
    query_posts($sectionarg); ?>

    But that completely destroys the original list of returned posts and replaces it with regular posts.
    So it appears that to maintain the original query you have to use the first approach but how then do you sort the returned posts?
    Any advice would be greatly appreciated.
    Thanks

    [1]: http://codex.wordpress.org/Function_Reference/query_posts

Viewing 3 replies - 1 through 3 (of 3 total)
  • Tracy Levesque

    (@liljimmi)

    🏳️‍🌈 YIKES, Inc. Co-Owner

    Hello.

    First off your should be using WP_Query instead of query_posts (Class Reference/WP Query)

    For a Custom Post Type you have to name it in your query.

    For example:

    <?php $myloop = new WP_Query(array ( 'post_type' => 'my_cpt', 'order' => 'ASC' ) ); ?>
    
    <!-- Loop starts! -->
    <?php while ($myloop->have_posts()) : $myloop->the_post(); ?>
    
    ...	
    
    <!-- Loop ends -->
    <?php endwhile;  ?>

    Where I have ‘my_cpt’ you should put your custom post type.

    Thread Starter dorich

    (@dorich)

    @tracy
    Thanks for your response, very much appreciated.

    The reason I didn’t use WP_Query is that I have shown that the posts I want have already be returned so it didn’t seem necessary to create a new query. Although obviously that is a way to solve the problem. However, I wanted to leverage the built-in functionality of WP and that was the reason for using the taxonomy template. The slug on the template defines the posts to be returned and so the problem seems to be how to get the returned posts sorted.

    Along with your solution I’ll note here for anyone else who encounters this type of problem that you can solve it be adding a function and using the ‘pre_get_posts’ hook. I didn’t develop this solution, but you can see the full answer here.

    Tracy Levesque

    (@liljimmi)

    🏳️‍🌈 YIKES, Inc. Co-Owner

    Oh, right right. I didn’t notice you were using the WordPress Template Hierarchy to grab the correct taxonomy. Sorry!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sorting Custom Posts On Taxonomy Template’ is closed to new replies.