Is there any possible way to sort a new Wordpress post query by the title, but numerically instead of alphabetically?

I have some titles that have a lot of the same name alphabetically, then have a number afterwards, so of course for example Wordpress is putting title12 ahead of title1.

$args = array( 'orderby'=> 'title', 'order' => 'ASC', ); $loop = new WP_Query( $args );

I know we have this functionality to sort titles in ascending order but it does not sort titles like that:- Title 1 Title 2

Please let me know if we any work around using wp query ? Thanks for your help in advance :)

  • are you asking how to do a natural sort, aka 1,2,3,...,9,10 rather than 1,10,2,3,30,31 etc?
    – Tom J Nowell
    May 7, 2014 at 15:37
  • Could you provide example of titles and wanted/actual sort orders?
    – Rarst
    May 7, 2014 at 16:35
  • I think I can do this and I am curious enough in a sort of "code golf" kind of way :) , but I don't have time right now. I will try to check back in later.
    – s_ha_dum
    May 7, 2014 at 17:37

2 Answers 2


Well, technically what WP is doing is correct. You can either rename your titles using prefixed 0's.

Title 001
Title 012 


You can use sorting using the publish date

Or you can use an order meta field and sort on that one using wp_query

  • 1
    +1. Better to use meta-data in the post, to store variables like this, than to go with Ugly post title formats, such as Some Title 001, just for the sake of organization. Using meta-data, you can retain all the flexibility, without compromising aesthetics. May 7, 2014 at 16:31
  • I agree Hybrid, that would be my choice aswell.
    – woony
    May 9, 2014 at 18:51

Your titles aren't numbers. You can't sort numerically if you don't have numbers. Where does "T" go in a numeric sequence?

However, if you have a consistent pattern that you can leverage to break apart the title-- I used a space character and assumed a pattern like "Title" + space + "number"-- you can do something like this:

function numeric_sort_title_field($fields) {
  global $wpdb;
  $fields .= ', ';
  $fields .= 'REVERSE(';
    $fields .= 'LEFT(';
      $fields .= "REVERSE({$wpdb->posts}.post_title),";
      $fields .= "LOCATE(' ',REVERSE({$wpdb->posts}.post_title))";
    $fields .= ')';
  $fields .= ') + 0';
  $fields .= ' AS number';

  $fields .= ', ';
  $fields .= 'REVERSE(';
    $fields .= 'LEFT(';
      $fields .= "REVERSE({$wpdb->posts}.post_title),";
      $fields .= "LENGTH({$wpdb->posts}.post_title) - LOCATE(' ',REVERSE({$wpdb->posts}.post_title))";
    $fields .= ')';
  $fields .= ') + 0';
  $fields .= ' AS title_text';

  return $fields;

function numeric_sort_title($orderby) {
  $orderby = ' title_text, number';
  return $orderby;

$q = new WP_Query(
    'post_type' => 'post',
    'posts_per_page' => -1
if ($q->have_posts()) {
  while ($q->have_posts()) {
    echo '<br>';

You should get posts sorted by the text portion of the title and then by the numeric portion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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