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.

4
  • 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' Commented Oct 18, 2017 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 Commented Oct 18, 2017 at 6:52
  • 1
    Can you add in your code for the 'ignore_sticky_posts' Commented Oct 18, 2017 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. Commented Oct 19, 2017 at 1:43

2 Answers 2

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.

1
  • 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. Commented Oct 19, 2017 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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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