Avatar of Adam
Flag for Canada asked on

Issues with custom post type permalinks in Wordpress!!

I have a custom post type I've set up on my site and seem to be running into issues regarding  permalinks.

Essentially, I have a page on my Wordpress site called "Publications". The page also has 5 sub-pages labelled:

- Articles
- Speeches
- Reports
- Letters
- Submissions

I have also created a custom post type and called it "Publications". The post type also has categories that correspond to the 5 sub-pages mentioned above (i.e. Articles, Speeches, etc.). Hopefully that all makes sense so far...

What I've then done is created a template for each of the sub-pages and used query posts to list the posts that fall under that particular category. So all posts with the category "Articles" are listed on the sub-page titled "Articles"…and so on… The permalink that appears for "Articles" at this point shows up like this:


This appears to be working, however, the issue comes up when a user clicks on a post link to take them to the individual post. Half of the time, I am receiving a "404 Page not found" message (but not all the time which is strange). When it does work, this is the permalink that appears:

www.mysite.com/publications/post-name (notice the permalink is missing the category or sub-page name— it should read: www.mysite.com/publications/articles/post-name)

I have done some research and a lot of people have mentioned that there will be conflicts if you have a page with the same slug as a category. I've tried playing around with this for quite some time but it just doesn't seem to be working the way I want it to. In the end, I want the individual posts to appear as though they are within the "Publications" section of my site. So I essentially want my permalinks to read like this:


I've been at this for quite some time now and am stuck. Does anyone have any idea what I need to do to achieve what I'm looking for? Any help would be greatly appreciated.  
WordPressWeb DevelopmentPHP

Avatar of undefined
Last Comment

8/22/2022 - Mon

do you have mod rewrite enabled on the apache server?  I have seen that same error for another issue involving the 'pretty links'  after enabling mod rewrite, fixed the issue

this link contains some info on that - http://www.wallpaperama.com/disp-post10.html

Thanks for the quick reply madhatter5501.

I do have mod_rewrite enabled on the server. I think part of the problem as mentioned is that I'm using the same slug for my page as I am for my custom post type (i.e. "publications"). And on top of that, I'm using the same slugs for the category names as I am for the sub-pages (i.e. "articles", "letters", etc.).

However, I can't seem to figure out another way around this. I want each single publication to appear as if they are within the "Publications" section of my site. If I change the slugs though, I will get a different permalink and will throw some other elements off on the page (i.e. the sub-nav for the "Publications" section disappears the breadcrumbs will no longer indicate that the post is within "Publications").

Does that make sense? Do you have any idea of a way around this? Not sure if I just misunderstanding something or not.

Try adding this to your functions.php file:
flush_rewrite_rules( false );

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Thanks for the reply jeremyjared74, but I've tried that too and didn't have any luck.

I've also found this:

add_action( 'init', 'create_my_post_types' );

function my_rewrite_flush() {
register_activation_hook(__FILE__, 'my_rewrite_flush');

Open in new window

I know a lot of people have had similar issues and the flush seemed to fix the problem. Initially, I was getting "404 Errors" when I tried accessing www.mysite.com/publications/articles (for example), and the flush helped resolve that. But I'm still having issues when trying to access individual posts.

What is your permalink structure?

There are articles describing why wordpress's permalink structure when using certain structures is bad, depending on your posts/categories/etc it can't figure out what you are looking for... or figures it out wrong.  http://dougal.gunters.org/blog/2009/02/04/efficient-wordpress-permalinks/

When you create a post it should show you what the permalink will be. Does it look right on your posts if you edit a post?

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Here is my permalink structure (this is for regular posts though—not sure if it applied to custom post types):

Permalink structure
Here is the rewrite rule I created when registering my "publications" custom post type:

'rewrite' => array( 'slug' => '', 'with_front' => false )

Open in new window

And here is what appears in the slug when editing a "publication" post:

Slug when editing a post
Right now, when I try to access the post, I am getting a "404 Error" message. But as mentioned, the permalink structure is not quite what I want here anyway. For this example, I want the slug to read "publications/articles/sample-article/". For a post under the "Speeches" category, I want it to read "publications/speeches/sample-speech"...

I know, it's a bit confusing (I've been wracking my brain over it for a few days now). If you have any other thoughts, I'm open to any suggestions.

I believe if you wanted it to be as publications/speeches/sample-speech when you register the post type you would put

'rewrite' => array( 'slug' => 'speeches', 'with_front' => false )

The problem is that "speeches" is a category of "publications" (which is the custom post type). So if I set the rewrite to: 'rewrite' => array( 'slug' => 'speeches', 'with_front' => false ) as you suggest, all of my publications will use the slug "speeches".

What I'm need though is this:


All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

If you want to have a category called publications and a page called publications, then you are in trouble if you want your permalink structure to include category as part of it. What you can do however is make a custom-category-template so that the publications category page can look however you need it to and contain whatever you want. http://codex.wordpress.org/Category_Templates    I think this would resolve your issue, as it would allow the subcategories and posts to display with their permalinks as intended as well as give you control over how the publications category page looks.

Why did you create a custom post type instead of using categories?

Thanks for the reply jrm213jrm213.

I think you may be mixing up a few things though. "Publications" is the name of the custom post type and the categories associated with that post type include "Articles, Speeches, Letters, etc.".

On my site, here is the page structure for the "Publications" page:

Publications (parent page)
> Articles (sub-page that lists all "publication" posts with the category "Articles")
> Speeches (sub-page that lists all "publication" posts with the category "Speeches")
> Letters (sub-page that lists all "publication" posts with the category "Letters")
> etc.

There are actually two other custom post types on my site which is why I took this route (or else it would have been too confusing to try and organize everything with a whack load of categories). I am just using this one post type to try and keep things as simple as possible.

Anyway, I think you're right in the fact that having the same slug for the page and custom post type or category is going to cause a problem (which it obviously is). But again, my initial question is how do I make it so that my posts appear within the "Publications" section of my site. If I use your suggestion, even if I create a template and make the post "look" like it is in the Publications section, the actual site structure won't reflect that. I am using a sub-navigation menu within "Publications" as well as breadcrumbs. So the sub-nav won't appear once you are outside of the Publications section (unless I hard-code it onto the category template) or the breadcrumbs won't work properly if I take this approach. Does that make sense?

I appreciate your help jrm213jrm213 and I apologize if I'm further confusing things. This has been quite frustrating. I know a lot of people have had similar issues but I can't seem to find someone who's had the exact same problem as me.

If you have any other suggestions, I am obviously open to anything. Thanks again.

Hi, no problem, I just wanted to make sure you had a reason for using a custom post type and that it wasn't just something you found and thought would work for your site when a category could do the same.

If you aren't using categories for your posts, you might try making with_front true, that should place the permalink structure in front of the slug. Though that still might not get you the url structure you want.

"'with_front' - allowing permalinks to be prepended with front base (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/) - defaults to true " 
from: http://codex.wordpress.org/Function_Reference/register_post_type

Another thing I noticed was that as part of the register_post_type function you can set public to true and your custom post type should show up in nav menus.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Hmm...I didn't realize that you can add categories to the menu. That actually makes things much easier then the set up I had. Thanks!

However, one problem. As mentioned, my "Publications" section uses a sub-nav in my sidebar and shows breadcrumbs above the content area. I just replaced all the "sub-pages" under "Publications" with categories (within the main "Menu" as you recommended). Everything looks fine from the main nav, but the side-nav has now disappeared (I was using a function that listed "pages"—but as they no longer exist, the sub-nav is gone). And the breadcrumbs are only showing the name of the category rather then "Publications > Category Name". Also, I have a dynamic sitemap on another page that no longer shows anything underneath the "Publications" section.

I would much rather use this set up then what i had before but I would need to get that other stuff to work properly too (to give the perception that all those categories are within the "Publications" section of the site). Any ideas?
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Hmmm....I guess my only concern is the sitemap. Unless I manually create it (which I would prefer not to do), it's not going to show any sub-pages under Publications.

Anyway, I'll give it a go and see what I can come up with. I'll keep you posted. Thanks again for all your help.

Thanks for the help jrm213jrm213. The solution isn't perfect and there are a few other work-arounds—but it works for the most part.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes