2

I have a custom post type called 'projects' (pastebin) and I have a page called Projects set up in my dashboard which is set to display the Projects template. (pastebin)

The problem is when I use WP_PageNavi to incorporate pagination and click on page 2, I get an error 404. I read somewhere that this is because custom post types and pages can't have the same name. And I've found that to be true because I tried changing the page name to "Projects 2" and it worked.

But when I change the name of the custom post type, that gets reflected in the url. So if I change the custom post type to 'projects2' instead of just 'projects', the url now looks like this: mysite.com/projects2/single-post

I have my custom permalinks set to /%category%/%postname%/ by the way.

Likewise, I can't change the name of the page to 'Projects 2' for aesthetic reasons.

So how would I get around this? This has been driving me crazy for many hours. I would really appreciate any helpful advice!

4
  • I guess you have a conflict in your rewrite rules for your custom post type and your page. Can you try my Rewrite analyzer plugin and tell me what you get for the URL projects/page/2/? It should be pagename=projects and paged=2, not projects=page/2/. (Use @Jan when you reply in a comment and I get a notification.)
    – Jan Fabry
    Commented Feb 17, 2011 at 14:51
  • Thanks @Jan. I'm not sure how I'm supposed to answer so I took a screenshot: img836.imageshack.us/img836/2760/rewrite.gif
    – J82
    Commented Feb 17, 2011 at 19:20
  • That's great, and it's the problem I suspected. But no problem, we can solve it. It will have to wait till tomorrow for me, it's late here and I'm typing this on my phone. Is this WP 3.0 or 3.1 already?
    – Jan Fabry
    Commented Feb 17, 2011 at 20:26
  • @Jan Great! That is great news. Yes, I'm running the latest 3.0.5. Thank you for your help!
    – J82
    Commented Feb 17, 2011 at 20:40

2 Answers 2

3

As you can see in the rewrite analyzer, /projects/page/2/ sets projects=page&page=/2 instead of pagename=projects&paged=2 as we would like. So you just need to add a rewrite rule for this special case (in the register_projects() function, after you register your custom post type, would be a good place):

add_rewrite_rule( 'projects/page/([0-9]+)/?$', 'index.php?pagename=projects&paged=$matches[1]', 'top' );

Remember to flush your rewrite rules, by visiting the Permalinks page or by some other way.

5
  • thank you so much. It worked! This may be a dumb question but will this continue to work for pages 3, 4, 5, etc? Also, if I register a different post type in the future, do I have to add a similar rewrite rule?
    – J82
    Commented Feb 18, 2011 at 8:16
  • @John: The [0-9]+ matches anything from 0-9 ([0-9]) multiple times (+), so yes, it will work for higher numbers. If you add new post types you will have to do this again for each taxonomy. However, if you wait a bit longer and can use WordPress 3.1, you should register your custom post type with the new has_archive argument, which will create the archives without you having to create a "fake" page with a template.
    – Jan Fabry
    Commented Feb 18, 2011 at 8:20
  • Thank you I will keep that in mind. I hope there will be a tutorial on how to register custom post types with the has_archive argument. You're a life-saver!
    – J82
    Commented Feb 18, 2011 at 8:23
  • ok, thanks - but i dont read this in the question, i had understand, he search a way for an pagination with the plugin for custom post types and i think, the better way as this plugin is a core function. But it is right, the pagination with permalinks is with this function not ready.
    – bueltge
    Commented Feb 19, 2011 at 10:21
  • with the release of 3.1, I couldn't help but think back to this question and the use of the new has_archive argument. I tried adding 'has_archive' => true to the array of arguments; however, my projects page showed the archive page when I went to site.com/projects. Additionally, I tried 'has_archive' => projects and that did the same thing. I'm wondering how I can implement this?
    – J82
    Commented Feb 23, 2011 at 18:17
0

Its a better way, you build your own paginatin:

echo paginate_links(array(
  'current' => 5,
  'total' => 9,
  'prev_text' => '« Prev'
));

or via custom solutions, see this 2 posts:

4
  • I keep wanting to recommend that function myself, only problem is it's not the easiest function to make sense of(you get my vote though, i'd love to see users utilising this function alot more, instead of choosing to use pagenavi). Small note to others reading, it's the very same function you'll see used extensively throughout the WordPress administration area.
    – t31os
    Commented Feb 17, 2011 at 11:51
  • But I don't think the problem here is creating the navigation links, but handling the incoming URL. Both functions will probably generate the link /projects/page/2/, but I doubt any of them changes the rewrite rules to make it work.
    – Jan Fabry
    Commented Feb 17, 2011 at 14:21
  • Yes, I am all for creating my own pagination but as @Jan mentioned, won't both functions generate the same link?I'm wondering how others have handled this problem. Some have reported to have changed the name of the post type to its singular form (e.g. project) while keeping its plural form (e.g. projects) for the name of the page. However, if I change the name of the post type to singular, that gets reflected in the url as I mentioned (e.g. mysite.com/project/single-post) which then messes up the pagination since the url should be mysite.com/projects/single-post.
    – J82
    Commented Feb 17, 2011 at 19:03
  • By the way, thank you. I've followed your advice in using paginate_links and have gotten rid of pagenavi :)
    – J82
    Commented Feb 20, 2011 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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