Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

A B C ... X Y Z

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 :)).

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
This is definitely the ideal route to take. However, keep in mind, with thousands of posts; This could be quite a cumbersome task. You would need to initially query all posts beginning with each letter of the alphabet, and update each post with the proper taxonomy term, matching the respective letter of the alphabet. Otherwise, manually updating each individual post would take centuries to complete. –  Michael Ecklund Oct 5 '12 at 18:20
    
Yeah, that's a HUGE downside, these posts will be added by TOTALLY ignorant folks and I can't count on them when it comes to setting right taxonomy while creating new post. I'm not sure if querying all posts and updating them every time someone edits/creates something will be efficient? –  Wordpressor Oct 5 '12 at 18:46
1  
No, that would not be an issue at all, hide the UI and automate the process.do it on the save and update post hooks, doing it manually would be absurdly expensive –  Tom J Nowell Oct 5 '12 at 19:05
    
Tom J Nowell, it works like a charm, but I have one serious issue and I'm not sure how to manage this - some of the words will start with letters such as en.wikipedia.org/wikiĄ - I'm unable to set terms for these. Here's some code and explanation: pastebin.com/cHxbjVFH , why is that happening? I'm using WP pack dedicated for this language. –  Wordpressor Oct 10 '12 at 20:18
    
are you talking about accents and umlauts etc etc? If so then that's something that deserves a whole new question in its own right, and is not a trivial question to ask. It's likely it is also a general PHP question and so Stack Overflow would be the best place to ask it –  Tom J Nowell Oct 10 '12 at 22:25
show 2 more comments

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>
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.