Support » Developing with WordPress » Alphabetical category list without “The” or “An”?

  • Hi there! I don’t know if this is possible:
    My posts are alphabetically listed when a category is clicked. Is there a way to exclude “The” “A” or “An” from the alphabetizing?
    Sure, I could title my post “Title, The” to solve that problem. But since they are titles of movies and books it would be easier for readers to find them without 50% being listed under “T” for “The”.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You can use trim in php (, and then set the output as an array variable, then sort the array in php (

    8rittany — it would take a bit of work (depending on how may posts you have) but the AZIndex plugin allows you to sort posts into an index based on post titles, excerpts, or custom fields. So you could add modified titles of a book or movie (e.g. “Shining, The”) to a custom field or excerpt and then sort on those, leaving the original titles of your posts alone.

    The plugin is:

    I don’t believe trim would be a good choice for cleaning the title. This is best handled by a regular expression. For example:

    $wordlist = array(“the”, “a”, “an”);
    $string = “an animal in the house\n”; // example string
    $pattern = “/^(” . join(“|”, $wordlist) . “) /i”; // build the pattern
    echo preg_replace($pattern, “”, $string);

    The pattern used in this example is “/^(the|a|an) /i”, which translates to “match beginning of string only, “the” or “a” or “an” followed by a space.

    Delete the “an” and run it again. The “an” from the word “animal” is left intact as is the word “the” in the middle of the string.

    I used an array to hold the word-list as it can easily be converted to a function that obtained the words from a user setting if this were some kind of plugin.

    Hi Ogre, this is something I want to accomplish on my blog as well– sorting the category archives by name without “the,” “an,” or “a” factored in. Could you please go into a little more detail about how exactly I would use that code you offered? Also, would that interfere with the prev/next navigation of the archives?

    The code is not actually useful as-is. You would either need to hack the wp code or make a plugin that does the job. Of-hand, I don’t know where to make the necessary changes. Perhaps when I have some free time I’ll poke around a bit.



    You could probably accomplish this using wp_list_categories() and setting the echo parameter to 0 so that it would keep it in a variable. Of course I’m not sure if that just throws the list in a variable or if it is an array (Haven’t tested it out) so there would probably need to be some string trickery in there needed to use natural WP functions.

    How to Pass Tag Parameters – May not be a ton of help but thought it may be useful.

    These are just off the top of my head. Hope it helps get the ball rolling for you.



    wp_list_categories() returns a string if I’m not mistaken.

    On the other hand I’m certain that if you want categories returned as an array, you’d use:




    You’re right, wp_list_categories() does return a string but you can throw it in a variable instead of having it echoed out.

    get_categories(), like dy_dream says, would return it in an array. Filtering from there would be a little tricky as it would require ignoring certain words in ordering but still including them in the echoed version.



    This could be handled using ‘usort’ (sort with user-defined compare function). Just have the compare function strip out the words.

    Completely untested code!

    function mycompare($a, $b) {
      $pattern = "/^(the|a|an) /i";
      $a = strtolower(preg_replace($pattern, "", $a));
      $b = strtolower(preg_replace($pattern, "", $b));
      return strcmp($a, $b);
    $categories = get_categories(); // I'm not sure of the parameter requirements

    The difficult part is figuring out where to stick the code.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Alphabetical category list without “The” or “An”?’ is closed to new replies.