Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(){
    $catHidden=get_cat_ID('hidden');
    $myquery = new WP_Query();
    $myquery->query(array('cat' => "-$catHidden",'post__not_in' => get_option('sticky_posts')));
    $myrecentpostscount = $myquery->found_posts;
    if ($myrecentpostscount > 0){ ?>
    <ul>
    <?php 
    global $post;
    if(get_option('mySort') == 'asc'){
        $sortOrder='title';$sortDirection='asc';
    }
    $myrecentposts = get_posts
    (
        array
        (
            'post__not_in' => get_option('sticky_posts'), 
            'cat' => "-$catHidden",
            'numberposts' => get_option('cb2_latest_count'),
            'orderby' => $sortOrder,
            'order' => $sortDirection
        )
    );

    foreach($myrecentposts as  $idxrecent=>$post) { 
        //DRAW THE POST MENU
    }
wp_reset_postdata();
}
share|improve this question
    
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 '11 at 15:27
add comment

2 Answers

up vote 5 down vote accepted

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


Edit Added Answer

<?php 
$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
    the_title($k);
    the_content($k);
}
?>
share|improve this answer
    
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 '11 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 '11 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 '11 at 21:05
    
this should do it. (see edit.) –  Mild Fuzz Jun 23 '11 at 21:32
add comment

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));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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