Advertisement
vtxyzzy

Titles by meta values

Nov 3rd, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2.  
  3. // List post titles by Business Category, Subcategory
  4. $sql = "
  5. SELECT p.*,m1.meta_value as category, m2.meta_value as subcategory
  6. FROM $wpdb->posts p
  7. JOIN $wpdb->postmeta m1 ON (p.ID = m1.post_id AND m1.meta_key = 'Business Category')
  8. JOIN $wpdb->postmeta m2 ON (p.ID = m2.post_id AND m2.meta_key = 'Subcategory')
  9. WHERE p.post_type = 'post'
  10.   AND p.post_status = 'publish'
  11. ORDER BY m1.meta_value, m2.meta_value, p.post_title
  12. ";
  13. $all_posts = $wpdb->get_results($sql);
  14. $curr_cat = '';
  15. $curr_subcat = '';
  16. $in_list = false;
  17. foreach ($all_posts as $post) {
  18.    setup_postdata($post);
  19.    $this_cat = $post->category;
  20.    $this_subcat = $post->subcategory;
  21.    if ($this_cat != $curr_cat) {
  22.       $curr_cat = $this_cat;
  23.       if ($in_list) {
  24.          echo "</ul>\n";
  25.          $in_list = false;
  26.       }
  27.       echo "<h2>$curr_cat</h2>\n";
  28.       $curr_subcat = '';
  29.    }
  30.    if ($this_subcat != $curr_subcat) {
  31.       $curr_subcat = $this_subcat;
  32.       if ($in_list) {
  33.          echo "</ul>\n";
  34.       }
  35.       echo "<h3>$curr_subcat</h3>\n";
  36.       echo "<ul>\n";
  37.       $in_list = true;
  38.    } ?>
  39.    <li><a href="<?php echo get_permalink($postid); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  40. <?php }
  41. if ($in_list) echo "</ul>\n";
  42.  
  43. ?>
  44.  
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement