Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // List Posts in selected categories followed by Pages
- $cats_array = array(32,96,78); // The cat_id list
- $cats_string = implode(',', $cats_array);
- $cats_sort = array( 3, 1, 2); // The order to sort the categories
- $sql = <<<EOSQL
- SELECT p.*, 0 as rec_sort,
- IF(tt.term_taxonomy_id = $cats_array[0],$cats_sort[0],
- IF(tt.term_taxonomy_id = $cats_array[1],$cats_sort[1],
- $cats_sort[2])) AS cat_sort,
- tt.term_taxonomy_id AS cat_id,
- t.name as cat_name,
- t.slug as cat_slug,
- p.post_date AS post_sort,
- ' ' AS page_sort
- FROM $wpdb->posts p
- JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
- JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category')
- JOIN $wpdb->terms t ON tt.term_id = t.term_id
- WHERE p.post_type = 'post'
- AND p.post_status = 'publish'
- AND p.post_date < NOW()
- AND tt.term_taxonomy_id IN ($cats_string)
- 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
- FROM $wpdb->posts p
- WHERE p.post_type = 'page'
- AND p.post_status = 'publish'
- AND p.post_date < NOW()
- )
- ORDER BY rec_sort, cat_sort, post_sort DESC, page_sort
- LIMIT 0,30
- EOSQL;
- $rows = $wpdb->get_results($sql);
- if ($rows) {
- // Build an index
- $curr_cat = '';
- $name_slugs = array();
- foreach ($rows as $post) {
- if ($post->cat_name != $curr_cat) {
- $curr_cat = $post->cat_name;
- $name_slugs[] = array($curr_cat => $post->cat_slug);
- }
- }
- echo '<a name="index-top"></a><br />';
- echo "<ul>";
- foreach ($name_slugs as $name_slug) {
- $keys = array_keys($name_slug);
- $cat_name = $keys[0];
- $cat_slug = $name_slug[$cat_name];
- echo "<li><a href='#$cat_slug'>$cat_name</a></li>";
- }
- echo "</ul>";
- // Now the Posts and Pages
- $curr_cat = '';
- $return_to_index = '';
- foreach ($rows as $post) {
- setup_postdata($post);
- // Set up a header as an anchor link if this is a new category
- if ($post->cat_name != $curr_cat) {
- echo $return_to_index;
- $return_to_index = '<a href="#index-top">Return to Index</a>';
- $curr_cat = $post->cat_name;
- echo "<h2><a name='$post->cat_slug'><br />$post->cat_name</a></h2>";
- }
- // Output the Post/Page info
- echo "<p>CAT_ID:$post->cat_id REC_SORT:$post->rec_sort CAT_SORT:$post->cat_sort "; the_title(); echo "</p>";
- }
- echo $return_to_index;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement