WordPress.org

Ready to get started?Download WordPress

Forums

Orderby title doesn't sort numbers correctly (38 posts)

  1. deeltje
    Member
    Posted 2 years ago #

    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");
  2. vjpo
    Member
    Posted 2 years ago #

    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

  3. deeltje
    Member
    Posted 2 years ago #

    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]

  4. vtxyzzy
    Member
    Posted 2 years ago #

    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;
    }
  5. deeltje
    Member
    Posted 2 years ago #

    @vtxyzzy;

    Thanks, but it still gives me the following order:

    1
    12
    3
    4
    7
    8

  6. vtxyzzy
    Member
    Posted 2 years ago #

    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

  7. deeltje
    Member
    Posted 2 years ago #

    This is my entire code right now:

    http://pastebin.com/MCQZ1TLW

  8. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  9. deeltje
    Member
    Posted 2 years ago #

    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
  10. vtxyzzy
    Member
    Posted 2 years ago #

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

  11. deeltje
    Member
    Posted 2 years ago #

    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
  12. vtxyzzy
    Member
    Posted 2 years ago #

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

    Here is a screenshot of the results.

  13. deeltje
    Member
    Posted 2 years ago #

    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 ...

  14. vtxyzzy
    Member
    Posted 2 years ago #

    Did you add in the debug code?

  15. deeltje
    Member
    Posted 2 years ago #

    Yeah, here's my complete code:

    http://pastebin.com/jb9S7svS

  16. vtxyzzy
    Member
    Posted 2 years ago #

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

  17. deeltje
    Member
    Posted 2 years ago #

    Yeah that's all i'm seeying, could it be a php-setting?

  18. vtxyzzy
    Member
    Posted 2 years ago #

    I am using PHP V5.3.5. The PHP online manual says that it is supported in 4 and 5.

  19. deeltje
    Member
    Posted 2 years ago #

    I'm using PHP v 5.2.17 Shouldn't be a problem ... I'm baffled ...

  20. vtxyzzy
    Member
    Posted 2 years ago #

    So am I. I'm out of ideas - sorry.

  21. vtxyzzy
    Member
    Posted 2 years ago #

    I just noticed that I left off the parens after rewind_query().

    Try adding them.

  22. deeltje
    Member
    Posted 2 years ago #

    rewind_query();
       echo '<br /><h2>After numeric sort</h2>';
       usort($wp_query->posts,'my_post_sort');
       while (have_posts()) {
          the_post();
          the_title();echo "<br />";
       }
       echo '<br /><br />';

    As soon as I added the () I get a

    Fatal error: Call to undefined function rewind_query() in /public_html/client/wp-content/themes/client/gallery-loop.php on line 39

  23. vtxyzzy
    Member
    Posted 2 years ago #

    Now I really feel bad. It should be rewind_posts();. I don't know why I don't get an error with the other.

  24. deeltje
    Member
    Posted 2 years ago #

    You were right, but sadly enough I still get the same 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
  25. vtxyzzy
    Member
    Posted 2 years ago #

    Well, as a last resort, you could use leading zeros in the titles:

    000001 testes
    000012 aaaaa
    000020 dxfvvrer
    000004 xdfsd
    000045 asasd
    000007 aaaa

  26. esmi
    Forum Moderator
    Posted 2 years ago #

    Can I point out that:

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

    is perfectly correct from a programming pov? Those "numbers" are actually strings - not numeric integers.

  27. vtxyzzy
    Member
    Posted 2 years ago #

    You are correct, esmi, but the code we have been trying is supposed to extract the numbers from the titles and sort in numeric order.

    The problem is that the call to usort is being ignored for the OP.

    Here is the result of my test:

    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

    So the question is, Why is usort ignored?

  28. esmi
    Forum Moderator
    Posted 2 years ago #

    Where in the code do you change the extracted string numbers into integers?

  29. vtxyzzy
    Member
    Posted 2 years ago #

    Here is the code for the sort function:

    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) {
          $cmp = 0;
         }
       $cmp = ($akey < $bkey) ? -1 : 1;
       print_r("akey:$akey  bkey:$bkey cmp:$cmp<br />");
       return $cmp;
    
    }

    As you can see, it should be printing debug info, but it does not - the usort which calls this is being ignored for the OP but not for me.

  30. deeltje
    Member
    Posted 2 years ago #

    I'm using a theme called PicTree and it uses functions.php heavily.

    One of the included files in functions.php is max_posts.php which has a lot of query adjustments

    Heres the code in the max_posts.php file:

    http://pastebin.com/EauTJwJv

    I'm sure the entire system is being adjusted there and thus overwriting whatever where trying to do with the gallery_loop.php file ...

    Any suggestions there?

Topic Closed

This topic has been closed to new replies.

About this Topic