Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP Rewrite to remove part of slugs in permalinks ? WordPress

How can I use a WP Custom Rewrite to remove part of a slug, like how php's explode() works?

I want to have these URLs:
/reports/biotech/deals/
/reports/retail/deals/
/reports/finance/deals/

where
- "reports" is my Custom Post Type rewrite slug,
- the industries ("biotech", "retai", and "finance") are terms of my Custom Taxonomy (I already have a function to add them to the permalink),
- "deals" is the re-written slug that I'm asking about now.

But since slugs must be unique, I can't just type in "deals" for all three posts. I'd need to actually use slugs like:

- deals-1
- deals-2
- deals-3

So I'm looking for a way to remove the suffixes (the "-1", "-2", and "-3" parts) from each slug when displayed in the url. In php, I would say
$slug_array = explode('-',$slug);
$slug = $slug_array[0] . "/" ;

But I don't know how to accomplish this with Rewrites, or any other way.

Answers (3)

2011-12-04

Luis Cordova answers:

I think rather you would want to categorize them with something like tags or other a second subcategory/subterm taxonomy and not doing this artificial thing that may break.


Todd Zeldin comments:

Hi Luis. Thanks for your reply. If i understand you correctly, you're suggesting:

Instead of my initial approach, where the url "/reports/biotech/deals/" shows a single post of my "reports" cpt -- the one that has "biotech" as the term of the "industry" taxonomy, and a slug of "deals-1", but somehow hacking the slug to remove the "-1" when displayed in the url…

Instead, the url "/reports/biotech/deals/" would actually show a taxonomy archive page, where only one post would be found, having cpt = reports, industry = biotech, and report_type = deals. And instead of the template file showing only the excerpt and a "read more" link, it would show the full content of the post. This way the slug of the post being displayed is never actually shown in the url.

Is that right? Is that what you're suggesting? If so, how do i accomplish that? How would i write a loop query that parses the url correctly for this.. with a cpt and two custom taxonomies all pulled from the url?

2011-12-05

Maor Barazany answers:

Different terms in different/same taxonomies cannot have the same slug. The slug of a term must be unique.


Todd Zeldin comments:

Hi Maor. Thanks for your reply. I understand that my slugs must all be unique. That's why i'm asking about rewrites as a method of having different slugs:
- deals-1
- deals-2
- deals-3
which appear to the user as if they are the same: "deals".

Perhaps this is not possible -- if the rewrites happen BEFORE wp parses the url to query the database?


Maor Barazany comments:

It might be useful to think of implementing this with .htaccess rewrites, it might be that the rules should be added after the wp .htaccess rules, you should check this.
Anyway, a direction to that, might be something like this -


RewriteCond %{REQUEST_URI} ^/reports/biotech/deals-\d$
RewriteRule (.*)$ /reports/biotech/deals [R=301,L]


Which should rewrite any url of the type <em>domain.com/reports/biotech/deals-X</em> (where X is a digit number) to <em>domain.com/reports/biotech/deals</em>
I have not tested it, and it might be that WP will cause 404 error for that. Try checking it in your dev env and see.

2011-12-07

Mike Van Winkle answers:

Have you looked at the [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Rewrite"]]article in the codex[[/LINK]].

You should, in theory, be able to do something like this.


add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
function my_insert_rewrite_rules($rules) {
$new = array();
$new['(.*)/(.*)/deals$'] = 'index.php?post_type=$matches[1]&custom_taxonomy=$matches[2]&post_tag=deals';
return $new + $rules;
}

This example assumes you would replace "custom_taxonomy" with the actual rewrite variable of your custom taxonomy and that "deals" is a standard tag and not another taxonomy. If deals is another taxonomy then you would replace "post_tag" with the rewrite var.

Keep in mind that adding rewrite rules like this can be a little tricky because they can interfere with other rewrite rules that you've come to take for granted. For instance, you may end up having to add yet another rewrite rule for the individual posts in that post_type because the standard %%POSTTYPE%%/%%POSTNAME%%/ structure is now being interpreted as %%POSTTYPE%%/%%CUSTOM_TAXONOMY%%.