Display all posts starting with given letter?

I’m trying to build WordPress based dictionary, basically it will have 26 pages (one for each letter):

A B C … X Y Z

Read More

And every page will display all posts starting with given letter, so after opening the “A” page it should display something like:

aback

abacus

abalone

(…)
azure

I was thinking about custom post types at first, but creating and maintaining 26 custom post types sounds like an overkill.

What will be the most efficient way of sorting posts like that? Database query, splitting loop with PHP? I am aiming for 1000s of posts (yes, it has to be WordPress :)).

Related posts

Leave a Reply

3 comments

  1. Don’t use post types, use taxonomy terms!

    On save, set the object terms in an A-Z taxonomy, using the first letter of the post title. Make sure to force upper or lowercase for consistency. Make sure to create terms for each letter of the alphabet, and a term for numbers and other non alphanumeric symbols.

    This should be faster than querying the first letter of each post title, and it gives you a taxonomy you can do more stuff with, like tag clouds, or term lister widgets! It’s also a much faster way of determining which letters have associated posts and how many, without forcing the database to manually count, and you can use standard WordPress term APIs out of the box to do more complex queries, like posts starting with vowels

  2. If you don’t want to go with the preferred Taxonomy terms method, do this:

    <ul class="posts">
             <?php 
             global $wpdb; 
             $request = "a" // could be any letter you want
             $results = $wpdb->get_results(
                    "
                    SELECT * FROM $wpdb->posts
                    WHERE post_title LIKE '$request%'
                    AND post_type = 'post'
                    AND post_status = 'publish'; 
                    "
             ); 
             if ( $results ) 
             {
                foreach ( $results as $post ) 
                {
                    setup_postdata ( $post ); 
                    ?> 
                    <li>
                        ... loop stuff here (the_title, the_permalink) ... 
                    </li>
                    <?php 
                }
             } 
             else 
             {
                ?> 
                <div class="alert">No clubs found for that letter. Please try again, or use the search at the top.</div>
                <?php
             }
             ?>
        </ul>
    
  3. Check this article here: query_post by title?

    Or you can create pre-save helper using PODS 2 framework and save the first letter of the post title in some field and use simple WHERE condition.

    Or create a dropdown (another pod/contenttype) with all letters and create relationship to you existing posts content type (in pods 2 it is possible) and that’s it. So, before saving the dictionary term you select from dropdown box the letter you want to assign to this term.

    In Pods 2 you can add the extra field to an existing posts content type. This plugin/framework that is something like CCK + Views in the Drupal world is really awesome.

    Pods 2 is very helpful plugin.