I've built my own custom 'recent posts' widget to display some more content than the default core WP recent posts widget does. I'm struggling with controlling my list of entries. Currently, the widget always returns 1 additional post to the list selected. So if I select 1 post to show, it will actually show 2, and so forth. I'm not really sure where my code is going wrong:
// Run query to build posts listing
public function getPostsListings($numberOfListings,$showExcerpt,$showDate,$showFeatImage,$post_excerpt_length)
{
$post_listings = new WP_Query();
$post_listings->query('posts_per_page=' . $numberOfListings, 'ignore_sticky_posts' => 1);
if ($post_listings->found_posts > 0) {
echo '<ul class="posts_widget">';
while ($post_listings->have_posts()) {
$post_listings->the_post();
$listItem = '<li>';
if ( !empty($showFeatImage) && has_post_thumbnail() ) {
$listItem .= '<img src="'. get_the_post_thumbnail_url(get_the_ID(),'thumbnail') .'" />';
}
$listItem .= '<a href="' . get_permalink() . '">';
$listItem .= get_the_title() . '</a>';
if (!empty($showDate)) {
$listItem .= '<span class="post-date"> Posted: ' . get_the_date() . '</span>';
}
if (!empty($showExcerpt)) {
$listItem .= '<span class="post-excerpt">' .wp_trim_words( get_the_excerpt(), $post_excerpt_length). '</span>';
}
$listItem .= '</li>';
echo $listItem;
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p>No posts were found</p>';
}
}
Obviously there is more code to the widget than that, but I'm sparing it for the sake of brevity as I'm fairly sure my while loop is the core issue.
'ignore_sticky_posts'
is set in the query they'll be included and not count towards'posts_per_page'
ignore_sticky_requests
but it did not workignore_sticky_posts
and I think that is the core issue - Thanks everyone! I will put down the updated answer to the original question with the code re-organised to better support multiple params in the query.