Order by title without taking into account ‘the’

I currently have episodes from a TV Show that feature bands playing, so for example I have:

  • Moby
  • The Dandy Warhols
  • The Kooks

My wp_query looks like this:

Read More
$loop = new WP_Query(array('post_type' => 'episodio', 'cat' => '9', 'posts_per_page' => 90, 'orderby' => 'title','order' => 'ASC'  ));

But I want to order them like this, alphabetically, without taking into account ‘the’:

  • The Dandy Warhols
  • The Kooks
  • Moby

There are a lot of records in the database, so it’s not very practical and/or efficient to get everything into an array, remove ‘the’, order again and then going through the array to display the data.

Is this possible in WP_Query? Maybe through a filter?

Thanks a lot in advance! 🙂

Related posts

Leave a Reply

3 comments

  1. If you’re using the plugin SEO Ultimate (it’s popular and I recommend using it), it has a component called Slug Optimizer that strips those types of words out of the post-slug, so if the post title is “The Dandy Warhols”, the slug would just be “dandy-warhols”.

    Perhaps just order by the post-slug?

  2. This is untested, but you can try this query

    "SELECT post_title FROM wp_posts ORDER BY IF(LEFT(post_title,2)="A ",SUBSTRING(post_title FROM 3),IF(LEFT(post_title,3)="An ",SUBSTRING(post_title FROM 4),IF(LEFT(post_title,4)="The ",SUBSTRING(post_title FROM 5),post_title)))"
    

    It’s not the most efficient, but it should get the job done.

    The quickest way would probably be to have an additional column for the posts with these certain words stripped, solely for the sorting.

  3. $loop = new WP_Query(array(‘post_type’ => ‘episodio’, ‘cat’ => ‘9’, ‘posts_per_page’ => 90, ‘orderby’ => ‘title’,’order’ => ‘DESC’ ));

    This will get the post title descending from category id 9