Custom Post Type URL Rewriting?

  • I setup a custom post type for my portfolio projects. The main URL for this is located at /projects/

    Now I've also setup my blog posts permalink to /articles/*/ for the permalink structure. This means when I go to view a portfolio project the URL changes to /articles/projects/project-name/

    I know there must be a way to rewrite permalinks only for my projects custom post type. But I'm unfamiliar with the syntax in declaring the URL slug - would appreciate any help I can get!

  • 0x61696f

    0x61696f Correct answer

    8 years ago

    When you register the custom post type, you have to specify that the rewrite rule shouldn't be prepended with the existing URL structure.

    In short, this means that this line in your register_post_type call:

    'rewrite' => array('slug' => 'projects'),

    should turn into this:

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

    For more info, check out the rewrite argument from the codex entry on register_post_type

    edit: just make sure that, after updating the code, you flush the rewrite rules by visiting Settings > Permalinks. Otherwise you'll still see the old links.

    brilliant thank you! Just to clarify, all I need to do for flushing rules is to go to the Settings->Permalinks page and hit "Save Changes", correct?

    You don't even need to save changes. It's enough just to open the Permalinks settings page (that is, if your .htaccess file is writable. If not, press save changes and manually copy the code it returns in your .htaccess)

    This doesn't seem to work for me. My projects posts are still going to ``. I visited the Permalinks page too. What could be causing this? There aren't any rewrite rules in my `htaccess`.

    Wow, thanks that was the missing part! Visiting the permalinks page did not work, but just SAVING the current permalink settings worked :)

    I kept on changing things without flushing the rewrite rules. Thanks for the tip!

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM