I'm looking to draw a menu listing of the latest "n" number posts, where the number "n" will be a user defined setting stored as an option.

n = 5; //Pull the latest 5 posts from the database.

The script I'm currently using (below) pulls all of the posts from the database and then only displays the latest 5 out of that collection.

This works fine until I decided to sort the list by title. When I do that, rather than sort the latest 5 posts by title, its sorting the entire collection of posts, without regard to when they were posted

Any tips on how I can change this query to only pull the latest "n" posts, then sort by title of those 5 only.

(Note: I'm really interested in the get_posts() query inside the UL element. The first query ($myquery) is just here to make sure we have posts to display before proceeding.)

function recent_posts(){
    $myquery = new WP_Query();
    $myquery->query(array('cat' => "-$catHidden",'post__not_in' => get_option('sticky_posts')));
    $myrecentpostscount = $myquery->found_posts;
    if ($myrecentpostscount > 0){ ?>
    global $post;
    if(get_option('mySort') == 'asc'){
    $myrecentposts = get_posts
            'post__not_in' => get_option('sticky_posts'), 
            'cat' => "-$catHidden",
            'numberposts' => get_option('cb2_latest_count'),
            'orderby' => $sortOrder,
            'order' => $sortDirection

    foreach($myrecentposts as  $idxrecent=>$post) { 
  • I think the crux of the problem is that there does not appear to be a way to call just the latest 5 posts, then sort only those by title. When I apply a sort to the query, rather than first pulling the latest 5 posts and sorting those, it sorts on the entire collection of posts.
    – Scott B
    Jun 23, 2011 at 15:27

2 Answers 2


$yourquery = new WP_Query('posts_per_page=5&orderby=title'); would do that.

Edit Added Answer

$posts = new WP_Query('posts_per_page=5');
foreach($posts->posts as $post){
    $sorted[$post->ID] =  $post->post_title;
asort($sorted, SORT_STRING);

foreach($sorted as $k=>$v){
        //your loop -- use ID's for each item call
  • Thanks for the answer MF, but I'm not quite there yet. I think what might be confusing is that I'm really concerned with the 2nd query, get_posts(). Its still pulling from the entire collection of posts, not the latest 5
    – Scott B
    Jun 23, 2011 at 12:56
  • what don't you get them, then order them? If your problem is getting the wrong ones, get the right ones first, then put them in order.
    – Mild Fuzz
    Jun 23, 2011 at 19:17
  • Yes, that's what I'd like to do. However, Once you have the latest 5 posts, they are in post order (latest first). I want to then take those 5 posts and order them by alpha before writing them to the for loop. how do you then to the forloop what order to arrange them in?
    – Scott B
    Jun 23, 2011 at 21:05
  • this should do it. (see edit.)
    – Mild Fuzz
    Jun 23, 2011 at 21:32

use the following code and modify to your liking:

// grab latest five posts
$latestposts = get_posts(); // by default latest 5 is grabbed
// just grab post ids
$keys = array_keys($latestposts);
// grab those 5 posts but ordered by title
$fivepostsorderedbytitle = get_posts(array('orderby' => 'post_title', 'post__in' => $keys));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.