Custom post types and permalinks : What a mess !

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');

Read More

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

Related posts

Leave a Reply

5 comments

  1. Change this:

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

    To this:

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

  2. 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 ! 🙂

  3. 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);
      $wp_rewrite->flush_rules();
    }
    add_action('init','rewrite_flush'