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();
}