41

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!

2 Answers 2

66

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.

6
  • 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?
    – Jake
    Commented May 25, 2012 at 18:06
  • 4
    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)
    – 0x61696f
    Commented May 25, 2012 at 18:09
  • 3
    This doesn't seem to work for me. My projects posts are still going to example.com/projects/title-of-post. I visited the Permalinks page too. What could be causing this? There aren't any rewrite rules in my htaccess.
    – Desi
    Commented Jan 25, 2015 at 13:36
  • 1
    Wow, thanks that was the missing part! Visiting the permalinks page did not work, but just SAVING the current permalink settings worked :) Commented Feb 28, 2019 at 12:09
  • 1
    I kept on changing things without flushing the rewrite rules. Thanks for the tip!
    – Tan-007
    Commented Nov 14, 2019 at 17:03
18

I had this problem literally 3 days ago, then I stumbled across a series over at wp.tutsplus.com. I swapped my own code out to accommodate your question better, but this is what I ended up with after following the series. Also, keep in mind that this is untested.

// sets custom post type
function my_custom_post_type() {
    register_post_type('Projects', array(   
       'label' => 'Projects','description' => '',
       'public' => true,
       'show_ui' => true,
       'show_in_menu' => true,
       'capability_type' => 'post',
       'hierarchical' => false,
       'publicly_queryable' => true,
       'rewrite' => false,
       'query_var' => true,
       'has_archive' => true,
       'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes'),
       'taxonomies' => array('category','post_tag'),
       // there are a lot more available arguments, but the above is plenty for now
    ));
}

add_action('init', 'my_custom_post_type');

// rewrites custom post type name
global $wp_rewrite;
$projects_structure = '/projects/%year%/%monthnum%/%day%/%projects%/';
$wp_rewrite->add_rewrite_tag("%projects%", '([^/]+)', "project=");
$wp_rewrite->add_permastruct('projects', $projects_structure, false);

Theoretically, you could swap out whatever you want in the URL stored in the $projects_structure variable, what is there is just what I ended up using.

Good luck, and as always - make sure to come back and let us know how it worked out! :)

3
  • 1
    Answers that are just composed of links are generally considered unhelpful as those resources can (and probably will) cease to exist in the future. Summarize the content. Commented May 25, 2012 at 16:56
  • Fair enough, I'll work on a proper revision.
    – cmegown
    Commented May 25, 2012 at 17:16
  • 14
    There, now my answer contains similar code to working code that I have in a production environment that successfully rewrites a custom post type URL. Hope it proves to be more helpful!
    – cmegown
    Commented May 25, 2012 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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