WP_Query and posts_per_page

Problem

The following code does not seem to limit the posts to 3 posts.  Instead uses the wordpress setting of 10 posts:

// The Query
$the_query = new WP_Query( array(
    'nopaging'       => true,
    'posts_per_page' => 3,
    'post_type'      => 'post'
) );

WordPress codex says the posts_per_page will be overridden if it is on a feed.  I am using this on the homepage however.

Solution

I have yet to find out why this doesn’t work.  A WORKAROUND is to add a filter like so:

function returnlimit() {
    return 'LIMIT 3';
}
add_filter( 'post_limits', 'returnlimit' );

// The Query
$the_query = new WP_Query( array(
    'nopaging'       => true,
    'posts_per_page' => 3,
    'post_type'      => 'post'
) );
remove_filter( 'post_limits', 'returnlimit' );

source: http://stackoverflow.com/questions/7577213/posts-per-page-not-working-for-new-wp-query

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.