1

In the function below, the $myrecentposts variable holds a collection of the 5 latest posts. How can I take these 5 posts and reorder them before they are written to the for-loop so that they are ordered by their post-title index in the array?

function getLatestPostsAndSort($post)
{
    $myrecentposts = get_posts(
        array('post__not_in' => get_option('sticky_posts'), 
        'numberposts' => 5
    )
 //NEED TO RESORT THE ARRAY HERE BY [POST-TITLE]
    foreach($myrecentposts as  $idxrecent=>$post)
    {   
    ?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php 
    }
}

PS: the obvious suggestion might be to pull them from the database in the desired order. However, the get_posts() method offers no such option. The default order is by post date (latest first). If I change the order to "title", it does not take post date into consideration and rather just pulls the entire post collection and orders them by title.

hakre
  • 193,403
  • 52
  • 435
  • 836
Scott B
  • 38,833
  • 65
  • 160
  • 266
  • possible duplicate of [PHP sort multidimensional array by value](http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value) – thetaiko Jun 23 '11 at 21:28
  • Why is `the_title()` a function taking no parameters? – Matthew Jun 23 '11 at 21:28
  • @konforce - the_title() is a built in method of the $post object – Scott B Jun 23 '11 at 21:32
  • That's wordpress. the_title() refers to a global variable named $post. - hi scott btw. doesn't wordpress answers work any longer? – hakre Jun 23 '11 at 21:34
  • @Scott B, is that the actual code? Because as written, it's simply a global function not attached to any object. Is `$post` an object or an array? The answer depends on what `$post` really is... Giving us the response of `var_dump()` on it should help. @hakre, Okay, that's weird. – Matthew Jun 23 '11 at 21:34
  • @hakre - hi there. Better answers here on PHP centric ?s :-) – Scott B Jun 23 '11 at 21:36
  • @konforce: $post is a global object. – Scott B Jun 23 '11 at 21:39
  • @Scott B, my answer should work for you then. You may need to alter it a bit to reflect what the `$post` object actually looks like. – Matthew Jun 23 '11 at 21:41

3 Answers3

2

Generally speaking, you can sort an array like:

usort($myrecentposts, function($a, $b) {
   return strcmp($a['title'], $b['title']); 
});

You need to adjust title to be whatever the actual name of the key is.

If this is wordpress, then based on a quick Google search, it looks like it might be:

usort($myrecentposts, function($a, $b) {
   return strcmp($a->post_title, $b->post_title); 
});

If you don't have PHP 5.3, then you'll need to move that anonymous function into a real one like:

function sort_post_by_title($a, $b) {
   return strcmp($a->post_title, $b->post_title); 
}
usort($myrecentposts, 'sort_post_by_title');
Matthew
  • 47,584
  • 11
  • 86
  • 98
1

http://php.net/manual/en/function.usort.php

You need to use usort();

Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
0

You can use php's usort function: http://php.net/manual/en/function.usort.php

xaph
  • 663
  • 5
  • 15