0

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.

  • Do you have any sticky posts? Unless 'ignore_sticky_posts' is set in the query they'll be included and not count towards 'posts_per_page' – Jacob Peattie Oct 18 '17 at 6:44
  • I may potentially have some sticky posts (I've migrated a whole bunch of these posts from a previous site that I did not develop). I just included the ignore_sticky_requests but it did not work – Ryan Coolwebs Oct 18 '17 at 6:52
  • 1
    Can you add in your code for the 'ignore_sticky_posts' – Sneha Agarwal Oct 18 '17 at 10:14
  • I've updated my answer to include the ignore_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. – Ryan Coolwebs Oct 19 '17 at 1:43
1

Anytime you call WP_Query, be sure to filter for only published posts. Also as a general guide, for any custom queries that you expect or could have more than one result for, try to always pass arguments for:

  1. Publish status
  2. Limit / number of results
  3. Field to sort by
  4. Sort order

Refer to the codex page (https://codex.wordpress.org/Class_Reference/WP_Query) for a full list of arguments you can pass.

| improve this answer | |
  • Thanks, but I don't think that is related to the problem I am experiencing. I've set a limit to whatever the user selects from dropdown in widget and it always displays +1 more. – Ryan Coolwebs Oct 19 '17 at 0:37
0

As @jacob and @Sneha mentioned earlier in comments - my issue was with sticky posts not counting towards 'posts_per_page. My updated code is below:

// Run query to build posts listing
public function getPostsListings($numberOfListings,$showExcerpt,$showDate,$showFeatImage,$post_excerpt_length)
{
    global $post;
    $args = array( 'numberposts' => $numberOfListings, 'ignore_sticky_posts' => 1);
    $recent_posts = get_posts( $args );
    if ( have_posts() ) {
        echo '<ul class="posts_widget">';
        foreach( $recent_posts as $post ) : setup_postdata($post);
            setup_postdata($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;
        endforeach;
        echo '</ul>';
        wp_reset_postdata();
    } else {
        echo '<p>No posts were found</p>';
    }
}

I did manage to solve it another alternative way by re appropriating code examples from here: https://themefuse.com/how-to-create-a-recent-posts-wordpress-plugin/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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