This is my WP loop:
// fetch latest news stories
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$posts = get_posts(array(
'numberposts' => 3,
'paged' => $paged,
));
// show results
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// display info about the post
}
}
Here’s the problem: only the first post is showing, and the pagination isn’t showing at all.
Interestingly, I use the same code (from // show results
down, since WordPress sets up the posts to display for search results automatically) to display the results on my search page, and it works perfectly.
I’d like to use this same display code (again, from // show results
down) on all my blog-related archive pages: date archives, main blog page, tag-based archive, etc.
I just need it to show more than one post and let my pagination show…
get_posts
returns an array of post objects. You cannot loop over that usingWP_Query
object methods. Even if you could, your loop will use the data in theglobal
variable$wp_query
as you did not tell it to do anything different. Andshowposts
is long since deprecated. In other words, your code is broken or flawed in several different ways.You could use
foreach
to loop over your array.You could clobber the global
$wp_query
object.Or create a new
WP_Query
object:Or, maybe, create a filter on
pre_get_posts
to alter the main query. If you want pagination to work, that is your best bet.