Possible Duplicate:
How To Modify The Loop in archives.php To Have 11 Posts Per Page and CSS Styling

I am using the following code on my archive.php template to list a few fields from a custom post type. It's an album review post type and this code pulls in the artist name, the album name and the album cover.

Is there any way to alter this code to tell wordpress to display a certain number of results?

The issue I have is that for all other archive pages the default (set to 10) is fine, but because I am only displaying a thumbnail and a name on the archive for reviews there's a lot of room to add more in.


1 Answer 1


You can hook into pre_get_posts and modify the query. Your hooked function will receive a WP_Query object as it's only argument. You can check to see if you're on a the correct post type archive and modify away.

Something like this:

add_action('pre_get_posts', 'wpse63675_pre_posts');
function wpse63675_pre_posts($q)

    $q->set('posts_per_page', 20); // or however many you want

You can drop the above in your functions.php file or a plugin. Use with caution.

I wrote a plugin that takes care of this for you. It does the above, but with a set of fields in the admin area.

  • +1 for the plugin and its code. Sidenote: Wouldn't it be better to hook the main file include process in a function hooked on plugins_loaded? Don't have to tell you why hooking stuff is a good idea ;)
    – kaiser
    Aug 30, 2012 at 22:52
  • That's true. I tend not hook includes into anything, to be honest: I make assumptions about what classes and functions are available elsewhere in the plugin, and I don't want to check to see if they are there. Better idea (maybe?): if(apply_filters('plugin_enabled_somefeature', true)) { /* load the files for the feature */ } Aug 30, 2012 at 22:56
  • Hm. Interesting approach. I normally (with everyday stuff), just hook my bootstrap class into plugins_loaded. There all the includes happen and if needed, I hook into init, as its present during ajax, admin and public requests. Then the routing runs via all the conditional hooks (load-{$pagehook} and such) or via conditionals/query args for public requests. So far I've never thought about such an approach, but there're core filters (for e.g. the one on the gallery shortcode cb fn), that behave like switches. Will have to think about this one. If you want to discuss it, reach me via mail.
    – kaiser
    Aug 30, 2012 at 23:35

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