8

Just wondering, how can remove slugs from some URLS. I'm using a custom post type called exhibitors, and some taxonomies to define those exhibitors (eg: featured-guests, publishers etc.):

Right now I have this: http://thisurl.com/exhibitor_filters/featured-guests/

I would like this: http://thisurl.com/featured-guests/

I've been playing around with the rewrite / slug in wp, but so far no dice.

Suggestions?

Thanks!

-Edit-

Fun times, now I'm just getting 404 errors for everything involving custom-post-types and taxonomy-terms. I'm going to see if I can at least get this thing to stop barfing-out so much first...thanks for your general suggestions guys.

5 Answers 5

7

This by default is not possible, and using the CPT and Custom Tax registration APIs, is not possible.

And for good reason.

It's all to do with permalink and slug clashes, and removing ambiguity. Admittedly there are cases where unique URLs that never overlap are not allowed by the system for this reason ( false negative ).

So I recommend you decide on a replacement for 'exhibitor_filters' such as 'exhibitors', or 'filters' and use that as your slug in the rewrite option when registering.

If you really want to do it the way you desire however, you will need to add rewrite rules. This can be problematic, as you run the real risk of clashes ( do we load the page 'about' or the exhibitor_filter term 'about'? ), and the ordering and priorities of your hooks will play a big role.

e.g.

function ex_rewrite( $wp_rewrite ) {

    $feed_rules = array(
        '(.+)'    =>  'index.php?exhibitor_filter='. $wp_rewrite->preg_index(1)
    );

    $wp_rewrite->rules = $wp_rewrite->rules + $feed_rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'ex_rewrite' );

The above code will work for individual terms, though it will need modifying for heirarchical URLs and taxonomies.

Place the code in functions.php of your theme, or your themes associated plugin.

Warning: You will need to be careful not to have clashing permalinks, and you will need to be aware of the ordering of which rules come first. Use the monkeyman rewrite analyser plugin to test this. You have been warned.

2
  • Does this still work? Does it need some sort of a return? It doesn't seem to be doing anything for me.
    – Garconis
    Commented May 12, 2021 at 17:25
  • yes, but you need to modify it to your specific use case, this answer was written for the OP, and so a straight forward copy paste won't work for you unless your custom post type and taxonomies have identical names to the original poster. This answer is not copypasta. Also note the caveats in the answer
    – Tom J Nowell
    Commented May 13, 2021 at 10:50
2

There is a plugin that will easily let you remove the taxonomy slug with a few clicks. Anyway as Tom said be careful for example not naming two different taxonomies terms the same or everything will brake.

Plugin url is http://wordpress.org/extend/plugins/wp-htaccess-control/

0

You should be able to specify this from your permalink section. You can set this via a custom permalink, but it seems like one of the default options "post name" should work for you. This can be found under settings, permalinks in the wordpress backend.

1
  • This isn't a post name, featured-guests its a taxonomy term. The issue I'm having is 404 errors with custom-post-types and taxonomy-terms.
    – Nadine
    Commented Feb 13, 2012 at 22:57
0

I found that answer here and used the code in the comments from Jonathan brinley. You should declare your taxonomy "exhibitor_filters" first, and then your CPT, setting its slug to %exhibitor_filters%.

Then all your CPTs and terms will be fine, including the term archives links. In my case I removed the CPT slug from the permalinks using the %taxo% in the CPT slug, and also worked except in the term archive, which still shows the CPT slug, but in your case seems what you want.

You seem to want archive pages, featuring all custom posts in the "featured-guests" exhibitor filter.

Good luck.

-1

Have you done:

'rewrite' = > array(
    'slug' => 'featured-guests',
    'with_front' => false )

and just add that to your register_taxonomy.

7
  • That gives me an internal server error.
    – Nadine
    Commented Feb 13, 2012 at 6:15
  • Is there a way to pass something like a blank slug? if I leave it blank, I get server errors as well.
    – Nadine
    Commented Feb 13, 2012 at 6:22
  • Can I see the full code, just to make sure it is implemented correctly. As far as I know, if the function fails it should not throw a 500 error.
    – Evan Yeung
    Commented Feb 13, 2012 at 18:37
  • register_taxonomy('exhibitor_filters','exhibitors', array( 'hierarchical' => true, 'label' => 'Exhibitor Type', 'query_var' => true, 'rewrite' = > array( 'slug' => 'featured-guests', 'with_front' => false ) ) ); I don't think this is right. Featured-guests is a term associated with exhibitor_filters. I just want it to not show the exhibitor_filters part in a link. Maybe that's not possible. I'll probably just hack a page together or something.
    – Nadine
    Commented Feb 13, 2012 at 23:02
  • I notice you are getting 404 errors with your permalink structure now. Have you flushed your rewrite structure? wpengineer.com/2044/custom-post-type-and-permalink
    – Evan Yeung
    Commented Feb 13, 2012 at 23:08

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.