I know that's a common problem with the custom-post-types, but I'm stuck there. I created a custom-post-type named 'cpt_soins'. I have no page with similar name (singular/plural)...

I tried with 'rewrite' => array('slug' => 'nos-soins-et-services');

when I registered my custom post type in order to display a more pretty url than "/?cpt_soins=soins-visage-a-la-rose"... But I'm stuck with a 404 error.

I want this ugly permalink: "http://localhost:8888/wordpress/?cpt_soins=soins-visage-a-la-rose" ---> transformed into: "http://localhost:8888/wordpress/nos-soins-et-services/soins-visage-a-la-rose"

I know i can use the "add_rewrite_rule() with regex". But I did'nt get working. Could you give me the code please for the functions.php in order to get my permalink ?

Thanks a lot


Change this:

'rewrite' => array('slug' => 'nos-soins-et-services');

To this:

'rewrite' => array('slug' => 'nos-soins-et-services', 'with_front' => true);

| improve this answer | |
  • I tried already front with true, but it did'nt worked. See my edited questions, I got it ! Everything rocks now... – Studiomambo Apr 28 '11 at 17:07
  • This would work but you may have written your code wrong. – xLRDxREVENGEx May 28 '11 at 18:43

You need to flush the rewrite rules after changing the 'rewrite' arg. This can be done most easily by accessing WP Admin -> Settings -> Permalinks.

| improve this answer | |
  • I have already force the flush, but without success (manually and within the code...) – Studiomambo Apr 27 '11 at 23:54
  • I've recently heard about a super-flush: Switch to default permalinks, save, then switch back to your chosen permalink structure. Don't know if it helps. – scribu Apr 28 '11 at 0:00
  • Also, it would be useful if you posted the entire post type definition (without the labels, though). – scribu Apr 28 '11 at 0:02
  • I already tried this. Without success... – Studiomambo Apr 28 '11 at 0:14

I've got it ! I used the plugin "Custom Post Type UI" in order to register my custom-post-type. I was stucked with my permalinks problems so I decided to redo everything manually without plugin. So I took the code from the codex to register my custom-pot-type and my taxonomies. I create a page template for displaying my custom-post-type, create some article under my custom post-type, and so on. Now everything work like a charm (category with my taxonomies and my articles asides my custom-post-types and top of that : the pretty permalinks).

I left the 'rewrite' => true. Nothing to do anymore... Don't bother with the permalinks. Just the for the fun I tried with 'rewrite' => array('slug' => 'my-word-for-my-permalink'), It works as well !

Don't forget to flush the permalinks manually and within your register function for cpt (at least once) in the functions.php...

Does anybody got some similar problem with the plugin "Custom Post Type UI" ? Which plugin do you use for create/manage your custom-post-types ?

Thanks everybody and have fun with your custom-post-types ! :-)

| improve this answer | |

Your pretty URL is not working because you have not introduced the line 'rewrite' => false, in the argsarray while declaring the Custom Post Type.

Use this code to solve your problem....Hope this works for you

//This function is to register the archive page of post type
function add_rewrite_rules($aRules) {
$aNewRules = array('nos-soins-et-services/?$' => 'index.php?post_type=cpt_soins');
$aRules = $aNewRules + $aRules;
return $aRules;
add_filter('rewrite_rules_array', 'add_rewrite_rules');

//This function is to set your Custom URL Structure and replace it with default structure
function rewrite_flush(){
  global $wp_rewrite;
  $cpt_soins_structure = 'nos-soins-et-services/%cpt_soins%';
    $wp_rewrite->add_rewrite_tag("%cpt_soins%", '([^/]+)', "cpt_soins=");
    $wp_rewrite->add_permastruct('cpt_soins', $cpt_soins_structure, false);
| improve this answer | |

It looks like you're using the default permalink setup. You need to enable URL rewriting with mod_rewrite to get 'pretty' permalinks. Read the codex permlinks page for more info.

| improve this answer | |
  • Nope ! I used this one : /%postname%. My rewrite -> slug didn't worked for my custom post type, so I turned it off with false. I know there's a solution like : add_action( 'init', 'ss_permalinks' ); function ss_permalinks() { add_rewrite_rule( '([^/]+)/all/?', 'index.php?view=all&service=$matches[1]', 'top' ); } add_filter( 'query_vars', 'ss_query_vars' ); function ss_query_vars( $query_vars ) { $query_vars[] = 'view'; return $query_vars; } But I'm a newbie with regex, I don't get the regex code with the add_rewrite_rule() function... – Studiomambo Apr 27 '11 at 23:49
  • if flushing the rules doesn't work as Scribu said, there must be an error elsewhere, you certainly should not have to add a rewrite rule manually. post all of your cpt code here so we can have a look. – Milo Apr 27 '11 at 23:59
  • code register_post_type('cpt_soins', array( 'label' => 'cpt_soins','description' => 'offres/produits menu Soins & services','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => false, code – Studiomambo Apr 28 '11 at 0:10
  • code'rewrite' => array('slug' => 'nos-soins-et-services'),'query_var' => true,'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),'taxonomies' => array('category','post_tag',),code – Studiomambo Apr 28 '11 at 0:11
  • code'labels' => array ('name' => 'cpt_soins','singular_name' => 'cpt_soins','menu_name' => 'Soins & services','add_new' => 'Nouvel article','search_items' => 'Search Soins & services','not_found' => 'Aucun article trouvé','not_found_in_trash' => 'Aucun article dans la poubelle','parent' => 'Parent Soins & services', ),) );code – Studiomambo Apr 28 '11 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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