Support » Fixing WordPress » How to sort posts with taxonomies?

  • Resolved ehannes


    I want to sort posts with taxonomies.

    I’ve made my own post-type and added two taxonomies called “day_type” and “time_type”. I now want to get all posts that have “day_type” == ‘day1’ and then order these posts by “time_type”.

    When the posts are printed to a page I just want to see the posts ordered by the “time_type” taxonomy and I don’t want the “day_type” to show at all. The “day_type” are only to exclude posts with anything other than “day1”.

    I have solved the first part, to only get the posts of day1 but then I’m stuck.

    <?php foreach(get_terms('day_type', 'orderby=slug&order=asc') as $type): ?>
    <?php if($type->slug == 'day1') { ?>
    /* content... */
    <?php } ?>
    <?php endforeach ?>


Viewing 5 replies - 1 through 5 (of 5 total)
  • I may be wrong, but I suspect the code you showed is inside the Loop. If that is the case and you are retrieving all posts then looping through all terms to show only the posts with ‘day1’, that is quite inefficient.

    Consider doing this by using a tax_query to select only the ‘day1’ posts in the query.

    To do the sort, you can probably use filters on the query to add the ‘time_type’ value to the fields and to alter the sort keys.

    The combination of these techniques will allow all selection and sorting to be done by the query.

    Your right, that code was inside the loop. I’ve changed it a bit, almost there, but I feel like my php skills isn’t enough…

    I know I need to run the $query outside the loop, I’m still running it from inside…

    /* Template Name: Speakers day test */
    $args = array(
            'tax_query' => array(
                            'taxonomy' => 'hannes_speakerday_type',
                            'field' => 'slug',
                            'terms' => 'day1'
    $query = new WP_Query( $args );
    if(have_posts()) : while(have_posts()) :
        <div <?php post_class(); ?>>
            <div class="post-cont">
                <h1><?php the_title();?></h1>
                <?php the_content(); ?>
                <?php foreach(get_terms('hannes_speakertime_type', 'orderby=slug&order=asc') as $type): ?>
                <div class="speaker-list news-index">
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <div <?php post_class(); ?>>
                        <div class="thumb"><?php the_post_thumbnail(); ?> </div>
                        <div class="post-cont">
                            <h3><?php the_title();?></h3>
                            <?php $content = get_the_content('L&auml;s mer >>');
                            $content = preg_replace('/<img[^>]+./','', $content);
                            $content = apply_filters('the_content', $content);
                            $content = str_replace(']]>', ']]>', $content);
                            echo $content; ?>
                        </div><!-- post_cont -->
                    </div><!-- post_class -->
                    <?php endwhile; ?>
                </div><!-- speaker-list news-index -->
                <?php endforeach; ?>
            </div><!-- post-cont -->
        </div><!-- post_class-->
    <?php endwhile;endif; ?>

    I still don’t know how to sort hierarchic taxonomies. But I changed strategy and now it’s working!

    Changed the “time_type” and “day_type” to “day1” and “day2”, made a page for the day1 posts and another for the day2 posts.

    But even though I would be glad to learn how to sort… Anyone?

    Here is the method I mentioned above – using filters. The code for the filter functions can be found here:

    I can’t test this code, but it should be close.

    Use the _join filter to join another instance of the taxonomy tables for the time_type (put in your own taxonomy for this).

    Use the _fields filter to add the term name to the query results.

    Use the _orderby filter to include the term name in the sort.

    Put this code just before issuing the new WP_Query():

    $mam_global_join = "JOIN $wpdb->term_relationships tr ON ($wpdb->posts.ID = tr.object_id)
       JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'time_type')
       JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
    $mam_global_fields = ", AS time_type";
    $mam_global_orderby = "time_type ASC, $wpdb->posts.post_title ASC";

    Put this just after the new WP_Query() to turn off the filters:

    $mam_global_join = $mam_global_fields = $mam_global_orderby = '';  // Turn off filters

    Then, if you need to, you can display the time_type as $post->time_type.

    Wow, thanks. I think that solution was a bit more complicated than the one I came up with. Need to study php better first to manage to filter functions…

    But thanks for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to sort posts with taxonomies?’ is closed to new replies.