A-Z index listing of all posts and pages with WordPress
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I want a way to list all of my posts, pages and custom post types posts to index in an A-Z listing. I'm looking for a function I can place in my functions.php and not a fully fledged plugin, something lighweight and clean and up-to-date with WP.

I'm thinking about using WP_Query() with a smart filtering.

awarded to hatasu

Crowdsource coding tasks.

3 Solutions


Hello there,

assuming you want to sort your posts by title, in A-Z (ascending order), it should be something like that :

<?php $query = new WP_Query(array('post_type' => array('post', 'page', 'custom_post_type'), 'orderby' => 'title', 'order' => 'ASC'));
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
// Content
<?php endwhile;endif; ?>

Is this helping you ?

This is partly working, as I have the same solution this far, but I need to group each post into the respective first letter. Like: A > Apple, Angel, Angle + B > Brown, Bear, Beer etc.
cphilippsen almost 7 years ago

Try to use this guid http://codex.wordpress.org/Function_Reference/query_posts
From this manual:
this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title:

query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );

Winning solution

Custom Page Template (Easy to edit)

<?php
/*
    Template Name: All posts & page
*/
?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
    <ul>
    <?php
        $str='z';
        $args = array( 'post_type' => array('post', 'page', 'custom_post_type'), 'order'=> 'ASC', 'orderby' => 'title' );
        $myposts = get_posts( $args );
        foreach($myposts as $post) :
            $title = $post->post_title;
            $s = 0;
            $db = 1;
            $title_f = mb_substr($title,$s,$db);
            if ($str != $title_f){echo "<h3>".$title_f."</h3>";}
    ?>
    <li>(<?php the_time('F j, Y'); ?>) <a href="<?php the_permalink(); ?>"><?=$title?></a></li>
    <?php
        $str=$title_f;
    ?>
    <?php endforeach; ?>
    </ul>
<?php endwhile; ?>

My fail again. Now tested with strange character (áéőúüöü), and really work. :)
When should you use WP_Query vs query_posts() vs get_posts()?

Ok, it's really need to work:

<?php
/*
Template Name: All posts & page
*/
?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
    $i = 1;
    $str='z';
    $args = array( 'post_type' => array('post', 'page', 'custom_post_type'), 'order'=> 'ASC', 'orderby' => 'title' );
    $myposts = get_posts( $args );
    foreach($myposts as $post) :
        $title[$i] = $post->post_title;
        $i++;
    endforeach;

    setlocale(LC_ALL, 'nl_NL');
    sort($title, SORT_LOCALE_STRING);

    for($j=1; $j<=count($title); $j++){
        $s = 0;
        $db = 1;
        $title_f = ucfirst(mb_substr($title[$j],$s,$db));
        if ($str != $title_f){echo "<h3>".$title_f."</h3>";}
            $page = get_page_by_title($title[$j]);
            ?>
            <li>(<?php echo $page->post_date; ?>) <a href="<?php echo get_permalink($page->ID); ?>"><?=$title[$j]?></a></li>
            <?php
            $str=$title_f;
        }
        ?>
    <?php endwhile; ?>
This is partly working, as I have the same solution this far, but I need to group each post into the respective first letter. Like: A > Apple, Angel, Angle + B > Brown, Bear, Beer etc.
cphilippsen almost 7 years ago
Also, I'm curious to know why you chose getposts() instead of WPQuery(). Convenience or speed? :)
cphilippsen almost 7 years ago
The revised solution are not working. And the solution must support ÆØÅ characters.
cphilippsen almost 7 years ago
This is working except for the output of special characters and the it is not ordering the alphabet correct. Know why? The bounty is yours if you can help me out on this.
cphilippsen almost 7 years ago
I try get first char with $title[0], but it's not work with Æ,Ø,Å,Ű etc. Now i use mb_substr(), becouse it's better. :)
hatasu almost 7 years ago
Almost there! Good job :) Is it possible to sort the alphabet like this: http://en.wikipedia.org/wiki/Danish_and_Norwegian_alphabet
cphilippsen almost 7 years ago
Thanks! Maybe possible... try add to wp-admin.php this -> "setlocale(LCALL, 'nlNL');" But bad sort is wordpress fail
hatasu almost 7 years ago
This did not help, I've troubleshooted and seen that Å gets rewritten to A within WP whilst placing on top of A, but there must be a fix for that? Otherwise I found a little bug in your code. See this screenshot: https://dl.dropbox.com/u/3803913/Screenshots/Screen%20Shot%202012-11-06%20at%2012.15.46%20PM.png Is it possible to place the whole Å link-section at the bottom?
cphilippsen almost 7 years ago
I've fixed the d and D bug with ucfirst(), like: $titlef = ucfirst(mbsubstr($title,$s,$db));
cphilippsen almost 7 years ago
It's done.
hatasu almost 7 years ago
Sorry, not working. 1) It sets Å to the third last section, it should be the last. 2) Permalink only works on Pages. Posts and Custom Posts are linking to the latest page. 3) 'iPad' and 'da Vinci' now has it's own 'i' and 'd' section. They should be with 'I' and 'D'. I've added ucfirst another place to see if it would help, but the post title uppercases like 'Da Vinci' and IPad'. 4) The script has removed a post named '1955'. 5) $str = 'z' means what? Norwegian alphabet ends on Å. 6) It echoes an empty section at the end.
cphilippsen almost 7 years ago
1.) Then alphabetical maybe not possible... 2.) Now i see, my fail... again :D But code #2 not work, use code #1. 3.) Change 'ucfirst' to 'strtoupper'. It's fix it. 4.) See 2. 5.) I just set value. And... use english, it's easier than norwegian :P
hatasu almost 7 years ago
1) Weird that it was nearly possible before, where it placed as third last. 2) Ok. 3) Done. Works. 4) Ok. 5) English is not an option on a full norwegian site ;) Could you also wrap the content like: https://dl.dropbox.com/u/3803913/Screenshots/Screen%20Shot%202012-11-07%20at%2011.11.40%20AM.png
cphilippsen almost 7 years ago
It says that you updated the solution a day ago, but the solution is still the old one?
cphilippsen almost 7 years ago
@hatasu Please provide the finished solution, so that the bounty is yours.
cphilippsen almost 7 years ago
View Timeline