Advertisement
vtxyzzy

Sort on taxonomy terms

Sep 3rd, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2.  
  3.    // Use filters to sort a query on terms of a selected taxonomy.
  4.    // See http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/
  5.    // for the code of the filters used.
  6.  
  7.    $taxonomy = 'madeof';
  8.    $mam_global_join = "JOIN $wpdb->term_relationships tr ON ($wpdb->posts.ID = tr.object_id)
  9.   JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '$taxonomy')
  10.   JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
  11.  
  12.    $mam_global_fields = ", t.name AS term_name";
  13.  
  14.    $mam_global_orderby = "term_name ASC, $wpdb->posts.post_title ASC";
  15.  
  16.    $args = array(
  17.       'ignore_sticky_posts' => 1,
  18.       'post_type' => 'post',
  19.    );
  20.    $q = new WP_Query($args);
  21.  
  22.    $mam_global_join = $mam_global_fields = $mam_global_orderby = ''; // Turn off filters
  23.    //print_r('<p>REQUEST:');print_r($q->request);print_r('</p>');
  24.    //print_r('<p>POSTS:');print_r($q->posts);print_r('</p>');
  25.  
  26.    if ($q->have_posts()) {
  27.       $current_term = '';
  28.       while ($q->have_posts()) {
  29.          $q->the_post();
  30.          if ($current_term != $post->term_name) {
  31.             $current_term = $post->term_name;
  32.             echo "<h2>TERM: $current_term</h2>";
  33.          }
  34.          echo "<p>TERM:$post->term_name &nbsp; &nbsp;  $post->post_title</p>";
  35.       }
  36.    }
  37.  
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement