WordPress: Change Posts per Page

Change Posts per Page

I know this is off topic for this blog. But, after spending most of the day trying to find out how to do this I had to put the answer where it can be used… and where I can find it when I forget how it is done.

The Problem

When building a blog people often want to control the number of posts that appear on a page. In some cases the first page of a group of posts needs more information than appears on the following pages. While the WordPress Admin controls allow one to set the number of posts per page, that setting is for ALL pages. The problem is in how to over ride the global setting when needed.

An additional part of the problem is the standard posts-per-page setting excludes sticky posts from its count.

The False Starts

Most of the solutions I could find try to use the WordPress function: query_posts() or get_posts(). These functions are the answer but failing to understand how they work creates lots of problems.

Both of the functions form query strings and make a call to the WordPress database. This means there is no variable that can be reset after the query is made that will change the number of posts appearing on the page. One needs to reconstruct the original SQL query that WordPress built, make a call, and get a new dataset. The function query_posts() replaces the dataset. So, all following loops will use the new dataset. The function get_posts() makes an additional dataset, which can be used for a secondary loop. That gets really complex as it involves the Pretty URL settings and .htaccess file entries. So, building a theme or custom page to work with all the possibilities gets messy in a hurry.

The Solution

The solution is easy and eloquent. It’s just hard to find.

query_posts($query_string . “&posts_per_page=$nu_items”);

The variable $nu_items is one I made. It is just an integer value, in my case 11. It is the $query_string value that is so hard to find. Or may be I just don’t know enough about WordPress yet.

The Query String

Those that work with web pages know about query strings. They are the part of the URL that comes after the ‘?’. Items in the string are separated by ‘&’.  Typically values are in the form: key=value.

WordPress does some changing and takes some shortcuts to keep the URL’s pretty so the search engines will like them. Here is an example:


Because I had access to a cheap Windows IIS server the page name index.php shows. On blogs on Linux systems it is hidden. So, the URL looks like:


The /tag/tutorials/ is a rewritten query string that in other web pages might look more like:


WordPress does its magic and we see a pretty URL and the value tag=60 is caught and saved into variables in the WordPress program.

When one clicks a link to move ahead to subsequent pages the URL grows. It starts to look something like:


Trying to keep track of all the changes and build new query strings just to change the number of posts per page gets complex. We need an easy way to add &posts_per_page=11 into the URL. Fortunately the value $query_string has all the needed stuff and we can just add our posts per page value to it.

query_posts($query_string  . “&posts_per_page=$nu_items”);

Place the query_posts() call ahead of the LOOP and any page navigation calls. It’s actually pretty easy.

Leave a Reply

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