Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom Post Types Permalinks mess
Tagged: custom post types, permalinks, plugins
- This topic has 10 replies, 2 voices, and was last updated 9 years, 5 months ago by
amandathewebdev.
-
AuthorPosts
-
August 18, 2015 at 11:18 am #162754
amandathewebdev
MemberHello all,
I'm in a pickle, I hope someone here can help me because I'm out of ideas. I downloaded the Custom Post Types UI (CPT UI) plugin, which is awesome. But I found an issue where I couldn't get the post type slug out of the URL. So I downloaded a plugin called "Custom Post Types Permalinks" (https://wordpress.org/plugins/custom-post-permalinks/) and that was cool because it let me add my CPT UI taxonomies to the URL. But it didn't have the option to get rid of the CPT slug from the URL.
So I found a THIRD plugin, and I knew it was a bad idea to download it because it hadn't been updated in over two years and everyone in the Support forum had the same issue with 404's (https://wordpress.org/plugins/remove-slug-from-custom-post-type/). This got rid of the CPT slug, (yay) but it caused a 404 (boo). I don't know why. So I deleted via FTP both the "Custom Post Types Permalinks" plugin and the "Remove slug from custom post type" plugin, reset the permalinks. I updated the permalinks settings to what they had been before the plugins existed (Custom Structure: /news/%category%/%postname%/). But I am still getting a 404 on all of my CPTs.
So a part of me suspects that this awful outdated plugin is still somehow effecting my site. Is that even possible? Another part of me suspects I messed something up. I could reset my dev site (thankfully this was all on a dev site!) and lose my work and start over (I'm willing to do this if it's the only option) but if this plugin is still effecting the site somehow in my database or something then that won't help.
And after I fix this, I'm still back in the same boat - the CPT slug will still be in the URL! Halp!
Thank you in advance for any advice!
http://64.91.255.161/montgomery-county-pa/window-replacement-job-in-narberth-paAugust 18, 2015 at 12:06 pm #162758Brad Dalton
ParticipantDid you resave your permalinks?
Also, using a plugin for CPT's is not needed.
The code enables you to control the slug:
add_action( 'init', 'executive_portfolio_post_type' ); function executive_portfolio_post_type() { register_post_type( 'portfolio', array( 'labels' => array( 'name' => __( 'Portfolio', 'executive' ), 'singular_name' => __( 'Portfolio', 'executive' ), ), 'has_archive' => true, 'hierarchical' => true, 'public' => true, 'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' ), 'taxonomies' => array( 'portfolio-type' ), ) ); }
You can see the rewrite parameter for the permalink slug built into the code:
'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
After adding code, re-save permalinks to flush the rewrite rules in .htaccess file which are cached.
August 18, 2015 at 1:03 pm #162765amandathewebdev
MemberHi braddalton - thanks for your response! I did try out the code you suggested and it worked to create a CPT for sure. But it's hard for me to tell if it's going to do what I need because the post I created is showing a 404 just like it is with the ones created by the plugin.
I did resave my permalinks. I also set them back to default, saved, set them back to custom, saved, a bunch of times while clearing my browser cache. I went into the post and saved, refreshed. I also reset my .htaccess file to the one I was using before the 404 errors started happening. I'm not sure what is causing this problem.
August 18, 2015 at 1:09 pm #162766Brad Dalton
ParticipantChange all instances of portfolio in my code to your CPT name and resave permalinks.
Also deactivate all plugins.
August 18, 2015 at 1:37 pm #162768amandathewebdev
MemberYou might find this as crazy as I do - I deactivated all plugins. Selected all, applied bulk action deactivate. Named the custom CPT code to have the same names and slug as the CPT I made with the plugin CPT UI. Saved permalinks again (this is: http://siteurl.com/%category%/%postname%/) and still getting a 404.
This didn't start happening until I downloaded the "remove slug from custom post type" plugin so I really think it has something to do with that - even though I deleted it. But how would I know?
I found this: http://dreamdare.org/tips-tricks/how-to-fix-wordpress-custom-post-type-permalink-404-error/
And I added the
flush_rewrite_rules();
they recommended after the code you provided. But that didn't work.August 18, 2015 at 1:50 pm #162770amandathewebdev
MemberNew input, I changed some settings in the CPT UI (plugin) and the 404 is gone. But the custom CPT code post is still 404ing, even when the CPT UI plugin is disabled.
August 18, 2015 at 2:27 pm #162775Brad Dalton
ParticipantIs the code generating a CPT using the same name as the plugin? The code is fine but i think one of the plugins is causing a problem.
August 18, 2015 at 2:31 pm #162776amandathewebdev
MemberThanks for sticking with me on this - when I added your code I noticed a post from a plugin I deactivated was in there. So I deleted it and added a new one, but this gets a 404.
Is it possible a plugin or plugins I deleted are still effecting things?
I also read that even with your own code, you can not get rid of the CPT slug from your URL. Is that true?
August 18, 2015 at 2:48 pm #162778Brad Dalton
ParticipantThe rewrite parameter enables you to customise the permalink slug.
You could hack it to remove the slug but it is not advisable as this will cause collisions.
You cannot use a Custom Post Type without a permalink slug as the rewrite parameter defaults to the cpt name.
August 18, 2015 at 3:19 pm #162781amandathewebdev
MemberOK - that hack to remove the slug is what I think caused all of my problems in the first place. I found a lot of stuff online about if you remove the slug, WP no longer recognizes the CPT as a CPT so you need to write a redirect rule. I have read from other sources though that you do not want to do this and it defeats the intended purpose of a CPT.
Do you know how I might solve my problem in some other way?
What I want to do is this. I want my URL to be
/county-name-state/post-title
. I want to group the posts by county. So I created a County post type. This holds all of the counties so that when someone goes to create a county post, they can select a county from the custom taxonomies. But by doing this, I wound up with a URL/cpt-slug/county-name-state/post-title
which is not good because Google will now think there is a parent page/cpt-slug/
which there is not. This will hurt SEO.The other reason I chose CPT (besides grouping) is that I want all posts to share a single template,
single-county.php
. So when I make a County post for Pennsylvania, New Jersey or Delaware, they all have the same custom fields, they all have the same template, and same URL structure:/county-name-state/post-title
So are Custom Post Types not the way to go for what I need? If so, why the slug in the URL? What is the proper way to do this?
Thank you again for all of your help!
August 20, 2015 at 11:41 am #163019amandathewebdev
MemberThanks to braddalton for his guidance. What I wound up doing is NOT removing the slug from the URL, but instead rewriting it. For anyone in a similar jam, here is what I wound up doing, and here is a link to the Stack Overflow I posted and got help from.
//Rewrite cpt slug to get rid of 404 generated by the function below add_filter('generate_rewrite_rules', 'my_rewrite', 9); function my_rewrite($wp_rewrite) { $wp_rewrite->rules = array_merge(array( '^([^-]*)-county-([^/]*)/([^/]*)/?$' => 'index.php?post_type=county-job&county=$matches[1]-county-$matches[2]&county-job=$matches[3]' ), $wp_rewrite->rules); } //No longer use links with /county-job/ as prefix add_filter( 'post_link', 'my_post_link', 10, 3); add_filter( 'page_link', 'my_post_link', 10, 3); add_filter( 'post_type_link', 'my_post_link', 10, 3); add_filter( 'category_link', 'my_post_link', 11, 3); add_filter( 'tag_link', 'my_post_link', 10, 3); add_filter( 'author_link', 'my_post_link', 11, 3); add_filter( 'day_link', 'my_post_link', 11, 3); add_filter( 'month_link', 'my_post_link', 11, 3); add_filter( 'year_link', 'my_post_link', 11, 3); add_filter('post_link', 'my_post_link', 1, 3); function my_post_link($post_link, $id = 0) { $post = get_post($id); if(is_object($post) && $post->post_type == 'county-job') { return str_replace('county-job/', '', $post_link); } return $post_link; }
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.