Support » Fixing WordPress » Orderby title doesn't sort numbers correctly

  • I’d like to sort my posts by number which is in front of the title

    1 post one title
    43 this is another post
    123 this is the last post

    I would like it to order in that order, but it seems to order reading letter for letter?

    1
    123
    43

    Anyone that can help me?

    At this moment I’m using the following code

    // query the posts
    query_posts("posts_per_page=".PER_PAGE_DEFAULT."&post_type=gallery&".GALLERY_TAXONOMY."=".$taxonomy_name."&paged=".$paged."&orderby=title&order=asc");
Viewing 15 replies - 1 through 15 (of 37 total)
  • Titles sorted in the alphabetical order that is different with numeric. May be you need to order posts by a custom field value, by using the 'meta_value_num' orderby parameter in the query. The Codex tip

    The problem is that the custom_field I’m using only works after the:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    This is my custom_field

    $nummer = get_post_meta($post->ID, $shortname.'schilderijnummer_value', true);

    And this is the query post:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Here is a bit of code that shows how to sort the posts array in the manner you described:

    // Test leading numeric title sort
    query_posts("posts_per_page=-1&caller_get_posts=1&orderby=title&order=ASC&cat=97");
    if (have_posts()) {
       usort($wp_query->posts,'my_post_sort');
       while (have_posts()) {
          the_post();
          echo "<br />";the_title();
       }
    }
    function my_post_sort($a,$b) {
       $akey = $a->post_title;
       if (preg_match('/^(\d+) /',$akey,$matches)) {
          $akey = sprintf('%010d ',$matches[0]) . $akey;
       }
       $bkey = $b->post_title;
       if (preg_match('/^(\d+) /',$bkey,$matches)) {
          $bkey = sprintf('%010d ',$matches[0]) . $bkey;
       }
       if ($akey == $bkey) {
          return 0;
       }
       return ($akey < $bkey) ? -1 : 1;
    }

    @vtxyzzy;

    Thanks, but it still gives me the following order:

    1
    12
    3
    4
    7
    8

    That is odd, it works for me. Here is the result of my test of the code:

    Sorted by title
    1 number test
    100 number test
    20 number test
    45 testing again
    another number test with no number
    number test without number

    After numeric sort
    1 number test
    20 number test
    45 testing again
    100 number test
    another number test with no number
    number test without number

    This is my entire code right now:

    http://pastebin.com/MCQZ1TLW

    Are your post titles entirely numbers – no text after the number?

    That is not what you showed in your original post, so the preg_match is not quite right. Change the preg_match patterns from this:

    (preg_match('/^(\d+) /', . . .

    to this:

    (preg_match('/^(\d+)/', . . .

    by taking out the one space before the trailing slash.

    There was no text after the numbers but there should be, so I changed that. The thing is, even with the space out of the code it still gives me the same reaction:

    1 testes
    12 aaaaa
    3 dxfvvrer
    4 xdfsd
    7 aaaa
    8 asasd

    Just a guess, but try adding $wp_query to your globals.

    global $imgDimensions, $substrExcerpt, $itemCaption, $shortname, $paged, $wp_query;

    Good guess, but still the same … sorry

    1 testes
    12 aaaaa
    20 dxfvvrer
    4 xdfsd
    45 asasd
    7 aaaa

    Here is the exact code I used (I added some debugging code).

    Here is a screenshot of the results.

    Sorted by title
    1 testes
    12 aaaaa
    20 dxfvvrer
    4 xdfsd
    45 asasd
    7 aaaa
    After numeric sort
    1 testes
    12 aaaaa
    20 dxfvvrer
    4 xdfsd
    45 asasd
    7 aaaa

    Darn, something is still going wrong …

    Did you add in the debug code?

    Yeah, here’s my complete code:

    http://pastebin.com/jb9S7svS

    Did you show all the output above? If so, the sort routine is not being called because the debug info is not shown.

Viewing 15 replies - 1 through 15 (of 37 total)
  • The topic ‘Orderby title doesn't sort numbers correctly’ is closed to new replies.