• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

WP_Query and posts_per_page

March 21, 2013 by Webhead

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

Filed Under: Coding Tagged With: php, wordpress

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools