12

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

3 Answers 3

10

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

8
  • 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. Commented Oct 5, 2012 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? Commented Oct 5, 2012 at 18:46
  • 3
    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
    Commented Oct 5, 2012 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. Commented Oct 10, 2012 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
    Commented Oct 10, 2012 at 22:25
4

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>
2
  • can you explain how this works? wouldn't it return any post that contains the letter? Commented Dec 18, 2018 at 5:56
  • it wouldn't. mysql uses % as wildcard. however i have never encountered $ in mysql queries. correctly it should read WHERE post_title LIKE 'request%'
    – brett
    Commented Feb 26, 2020 at 23:45
1

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.

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