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