• Resolved clickit

    (@clickit)


    Hi guys.

    I thought this one would be fairly simple but it’s proving to be a personal headscratcher.

    Please refer to:
    http://www.ata.neonsolutions.com.au/entertainment-options/

    Here I have a page template displaying posts by title.

    What I would like to do is display posts firstly by category name alphabetically, then by post name alphabetically; both in ascending order.

    ———————————

    My category list is:

    All Artists
    – Adult Contemporary
    – Comedians
    – Concept / Cover Bands
    – Country
    – Exclusive (33)
    – Jazz
    – Theatrical / Opera

    News (20)
    – Artist News (27)
    – Company News (28)

    ———————————

    I would like to exclude the category ‘Exclusive’ and all News categories. (Cat ids above)

    Any help greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is some sample code that I think will work, but you should probably modify to handle the ‘posts not found’ case. Put this in your functions.php:

    <?php function mam_posts_fields ($fields) {
       global $mam_global_fields;
       if ($mam_global_fields) $fields = "$fields, $mam_global_fields";
       return $fields;
    }
    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join = "$join $mam_global_join";
       return $join;
    }
    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_fields','mam_posts_fields');
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_orderby','mam_posts_orderby');
    ?>

    And use this in your template:

    <?php
        $mam_global_fields = 'mmt.name as catname';
        $mam_global_join = "JOIN $wpdb->term_relationships mmtr
           ON ({$wpdb->posts}.ID = mmtr.object_id)
        JOIN $wpdb->term_taxonomy mmtt
           ON (mmtr.term_taxonomy_id = mmtt.term_taxonomy_id AND mmtt.taxonomy = 'category')
        JOIN $wpdb->terms mmt ON (mmtt.term_id = mmt.term_id)
        ";
        $mam_global_orderby = 'catname ASC, post_title ASC';
        echo '<h2>Articles By Category</h2>';
        $args = array(
          'caller_get_posts' => 1,
          'posts_per_page' => -1,
          'category__not_in' => array(20,27,28,33),
       );
       $catname = '';
       query_posts($args);
        if (have_posts()) : while (have_posts()) : the_post();
           if ($catname != $post->catname) {
             $catname = $post->catname;
             echo "<br /><b>$catname</b>";
           }
           echo "<br />&nbsp; - ";?>
           <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php endwhile; endif; ?>
    Thread Starter clickit

    (@clickit)

    vtxyzzy,

    You may just have made my week. This has worked perfectly! Just a few minor modifications on my side to get pagination back up and running.

    Many thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort posts in page template by category, then title’ is closed to new replies.