• 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 - 16 through 30 (of 37 total)
  • Thread Starter deeltje

    (@deeltje)

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

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

    Thread Starter deeltje

    (@deeltje)

    I’m using PHP v 5.2.17 Shouldn’t be a problem … I’m baffled …

    So am I. I’m out of ideas – sorry.

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

    Try adding them.

    Thread Starter deeltje

    (@deeltje)

    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

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

    Thread Starter deeltje

    (@deeltje)

    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

    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

    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.

    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?

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

    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.

    Thread Starter deeltje

    (@deeltje)

    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:

    https://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?

    If you are showing the entire output of the run, and the debug info is actually not shown, then the usort is not being called. If that truly is the case, then the query adjustments are not the cause of the problem.

    Here is the actual output of my test:

    Sorted by title
    1 testes
    12 aaaaa
    4 xdfsd
    7 aaaa
    8 asasd

    After numeric sort
    akey:0000000004 4 xdfsd bkey:0000000012 12 aaaaa cmp:-1
    akey:0000000008 8 asasd bkey:0000000004 4 xdfsd cmp:1
    akey:0000000007 7 aaaa bkey:0000000004 4 xdfsd cmp:1
    akey:0000000001 1 testes bkey:0000000004 4 xdfsd cmp:-1
    akey:0000000007 7 aaaa bkey:0000000012 12 aaaaa cmp:-1
    akey:0000000008 8 asasd bkey:0000000007 7 aaaa cmp:1
    akey:0000000012 12 aaaaa bkey:0000000007 7 aaaa cmp:1
    akey:0000000008 8 asasd bkey:0000000012 12 aaaaa cmp:-1
    1 testes
    4 xdfsd
    7 aaaa
    8 asasd
    12 aaaaa

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