Advertisement
vtxyzzy

List Posts in selected Categories, then all Pages

Mar 16th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2.  
  3. // List Posts in selected categories followed by Pages
  4.  
  5. $cats_array = array(32,96,78);  // The cat_id list
  6. $cats_string = implode(',', $cats_array);
  7. $cats_sort = array( 3, 1, 2); // The order to sort the categories
  8.  
  9. $sql = <<<EOSQL
  10. SELECT p.*, 0 as rec_sort,
  11. IF(tt.term_taxonomy_id = $cats_array[0],$cats_sort[0],
  12.   IF(tt.term_taxonomy_id = $cats_array[1],$cats_sort[1],
  13.      $cats_sort[2])) AS cat_sort,
  14.   tt.term_taxonomy_id AS cat_id,
  15.   t.name as cat_name,
  16.   t.slug as cat_slug,
  17.   p.post_date AS post_sort,
  18.   ' ' AS page_sort
  19.    FROM $wpdb->posts p
  20.    JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
  21.    JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category')
  22.    JOIN $wpdb->terms t ON tt.term_id = t.term_id
  23.    WHERE p.post_type = 'post'
  24.    AND p.post_status = 'publish'
  25.    AND p.post_date < NOW()
  26.    AND tt.term_taxonomy_id IN ($cats_string)
  27. UNION (SELECT p.*, 1 AS rec_sort, 0 AS cat_sort, 0 AS cat_id, 'Pages' as cat_name, 'pages' as cat_slug, ' ' AS post_sort, UPPER(p.post_title) AS page_sort
  28.    FROM $wpdb->posts p
  29.    WHERE p.post_type = 'page'
  30.    AND p.post_status = 'publish'
  31.    AND p.post_date < NOW()
  32. )
  33. ORDER BY rec_sort, cat_sort, post_sort DESC, page_sort
  34. LIMIT 0,30
  35.  
  36. EOSQL;
  37.  
  38. $rows = $wpdb->get_results($sql);
  39. if ($rows) {
  40.    // Build an index
  41.    $curr_cat = '';
  42.    $name_slugs = array();
  43.    foreach ($rows as $post) {
  44.       if ($post->cat_name != $curr_cat) {
  45.          $curr_cat = $post->cat_name;
  46.          $name_slugs[] = array($curr_cat => $post->cat_slug);
  47.       }
  48.    }
  49.    echo '<a name="index-top"></a><br />';
  50.    echo "<ul>";
  51.    foreach ($name_slugs as $name_slug) {
  52.       $keys = array_keys($name_slug);
  53.       $cat_name = $keys[0];
  54.       $cat_slug = $name_slug[$cat_name];
  55.       echo "<li><a href='#$cat_slug'>$cat_name</a></li>";
  56.    }
  57.    echo "</ul>";
  58.  
  59.    // Now the Posts and Pages
  60.    $curr_cat = '';
  61.    $return_to_index = '';
  62.    foreach ($rows as $post) {
  63.       setup_postdata($post);
  64.       // Set up a header as an anchor link if this is a new category
  65.       if ($post->cat_name != $curr_cat) {
  66.          echo $return_to_index;
  67.          $return_to_index = '<a href="#index-top">Return to Index</a>';
  68.          $curr_cat = $post->cat_name;
  69.          echo "<h2><a name='$post->cat_slug'><br />$post->cat_name</a></h2>";
  70.       }
  71.       // Output the Post/Page info
  72.       echo "<p>CAT_ID:$post->cat_id REC_SORT:$post->rec_sort CAT_SORT:$post->cat_sort "; the_title(); echo "</p>";
  73.    }
  74.    echo $return_to_index;
  75. }
  76.  
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement