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

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Fixing WordPress 404 Custom Post Type Archive Pagination Issues with Posts Per Page

General · November 13, 2012

This is an annoying bug that has plagued WordPress for as long as I can remember, it’s rare that I run into it because I rarely need to have individual parts of a site control the number of posts I need to display with paging.

The Problem:

I have a custom post type called “project” as well as a blog section which just uses the default “post” post type. For projects I enabled archive support and then created a custom archive file called archive-project.php. I want to limit the number of projects being displayed per page to just 1 without changing the value in the WordPress reading options screen because I need that for the blog section, so naturally you’d expect to be able to do the following before the loop.

[code]

global $query_string;
$paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1;
query_posts($query_string.”paged=$paged&posts_per_page=1″);

[/code]

The above code snippet does indeed work for the first page, but visiting any subsequent pages like page 2 or page 3 will generate a 404 error. Although we’ve told WordPress we only want one project to show per page of our archive, once we hit the second page it thinks we’re using the options page value which by default is set to 10. The posts_per_page value is overwriting our value, this is an issue that many seem to encounter and most solutions don’t work or are heavily overcomplicated.

The quick fix is to set the value in the reading options section of the WordPress admin panel to be the number we want and then implicitly tell it to show more on parts of the site where we need more than one post shown at once like our blog section which we want to show 10 posts, not one.

The Fix:

The following code solution fixes the problem. Simply insert it at the bottom of your functions.php file and enter your post type name and required number to display. The code is simple enough to be tweaked for other purposes as well and if I can help, leave a comment and we’ll see what I can do.

Dwayne

Leave a Reply Cancel reply

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
mukesh kumar
mukesh kumar
6 years ago

Thanks it’s working

0
mukesh kumar
mukesh kumar
6 years ago

but some little issue i have only 2 pages but it display 5 pages in pagination

0
mukesh kumar
mukesh kumar
6 years ago

i have fix it just change in function.php function set_query_var(‘posts_per_page’, 4); Thanks

0
io
io
5 years ago

It’s working..,
thank’s..,

0
AKASH
AKASH
4 years ago

thanks

0
soumitra patra
soumitra patra
1 year ago

grate its working, thanks man.

0
hyetutors
hyetutors
1 year ago

but it is just change page number didn’t change the post, the post remain same on all pages.

0

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • How To Install Eufy Security Cameras Without Drilling or Using Screws
  • How To Get The Hash of A File In Node.js
  • Wild Natural Deodorant Review
  • The Most Common iPhone Passcodes (and how to guess them)
  • How to Record With the Neural DSP Quad Cortex in Reaper (DI and USB Recording)
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How To Paginate An Array In Javascript

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz