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.

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

share|improve this question
    
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 at 15:37
    
Could you provide example of titles and wanted/actual sort orders? –  Rarst May 7 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 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 

Otherwise

You can use sorting using the publish date

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

share|improve this answer
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. –  Hybrid Web Dev May 7 at 16:31
    
I agree Hybrid, that would be my choice aswell. –  woony May 9 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) {
  remove_filter('posts_fields','numeric_sort_title_field');
  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;
}
add_filter('posts_fields','numeric_sort_title_field');

function numeric_sort_title($orderby) {
  remove_filter('posts_orderby','numeric_sort_title');
  $orderby = ' title_text, number';
  return $orderby;
}
add_filter('posts_orderby','numeric_sort_title');

$q = new WP_Query(
  array(
    'post_type' => 'post',
    'posts_per_page' => -1
  )
);
var_dump($q->request);
if ($q->have_posts()) {
  while ($q->have_posts()) {
    $q->the_post();
    the_title();
    echo '<br>';
  }
}

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

share|improve this answer

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.