WordPress post permalinks for multiple categories, tags and taxonomy terms - wordpress

I'm creating a WordPress plugin and one of the requirements is that a custom post type (photo) with a custom taxonomy (photos) needs to generate multiple permalinks if the post is assigned to multiple terms... so for example if a post is assigned to two taxonomy terms ("birthday" and "wedding") both of the following permalinks should display the post:
/photos/bithday/photo/post-name
/photos/wedding/photo/post-name
I currently have the following rewrite rule defined, which is returning a 404:
add_rewrite_rule( 'photos/(.*)/photo/(.*)?', 'index.php?post_type=photo&taxonomy=photos&term=$matches[1]&pagename=$matches[2]', 'top' );
However, if I visit the following URL which matches the pattern of my rewrite rule:
/index.php?post_type=photo&taxonomy=photos&term=birthday&pagename=post-name
It returns the correct content, but only after redirecting to this URL:
/photo/post-name/?taxonomy=photos&term=birthday-cakes
I must be missing something, any help would really be appreciated!

I managed to get this working by changing &pagename= to &name=.

Related

WordPress: add_rewrite_rule with flexible path length? (Tag filter)

I need your help regarding WordPress URL rewrite. :o) At first, I am get this working:
add_rewrite_rule( 'category/([^/]+)/([^/]+)/?(?:page/?([0-9]{1,})?/?)?$' , 'index.php?category_name=$matches[1]&tag=$matches[2]&paged=$matches[3]', 'top');
This does some actions:
I can enter "blog.domain/category/category-name/" and get to the
category archive - with or without "page(d)"
I can also enter
"blog.domain/category/category-name/a-tag/" (or with "page/n/") and
get to the category archive, where the posts are filtered by an tag.
Now, I could extend this rewrite rule, f.e.:
add_rewrite_rule( 'category/([^/]+)/([^/]+)/([^/]+)/?(?:page/?([0-9]{1,})?/?)?$' , 'index.php?category_name=$matches[1]&tag=$matches[2]+$matches[3]&paged=$matches[4]', 'top');
This one will also works with "blog.domain/category/category-name/tag1/tag2/".
What I want to do is an pretty URL, so that users could filter the posts more and more by tags (f.e. /category/cars/racing/red/). Problem: for every new "path" (for category/cars/racing/ and category/cars/racing/red/) I need one add_rewrite_rule.
Is there any solution to reach this behavior with one single line? Maybe something like "there could be another path (as tag), but it mustn't"?

Wordpress custom post type in wrong taxonomies

I've add a custom post type with custom taxonomies. I've also added rewrite rules in order to handle following urls:
courses/languages/english/english-course
where courses is the base slug, languages and english are taxonomy slugs and english-course is my custom post. english-course is a child taxonomy of languages and english-course has been categorized in english-course.
My rewrite rule is:
courses/(.+)/(.+)/(.+)/?$ => index.php?thr_course=$matches[3]
Everything works fine but any categories are permitted using this syntax. All following urls are legitimate and work as well:
courses/languages/french/english-course
course/products/english/english-course
course/anycategory/anycategory2/english-course
My rewrite rule is pretty obvious: it only matches my post name ignoring which categories it belongs to.
Where and how should I implement a check in order to return 404
if post exists but parent taxonomies are wrong?
I think WordPrss will not give you a solution for your customization automatically. Fetch other parameters from like parent / child category from URL, similar to thr_course parameter. Use this parameter in your query to narrow down your result.
Hope this will help you.

Want to redirect a Wordpress URL to a specific page template and pass name of post

I've got a Wordpress site set up with a custom post type and custom taxonomy and custom URLs. However when I go to:
/custom-post-type/custom-taxonomy/custom-taxonomy-sub/postname
It 404s. I've tried everything and just want to create a redirect that grabs the postname and passes it to single-custom-taxonomy.php without changing the URL.
Some sort of redirect with a regex like /custom-post-type/(.*)/(.*)/(.*) and pass the third match to single-custom-taxonomy.php as a variable so it can get the post page.
How do I do this?
Figured it out:
function custom_rewrite_basic() {
add_rewrite_rule('^knowledge-center/(.*)/(.*)/(.*)', 'index.php?pagename=post_type=questions&questions=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_basic');

Custom rewrite rules on Wordpress

I have the following url query that is adding the parameter texas and the county harris to the permalink for a query. These are part of a custom taxonomy called geography.
xyz.com/dealers/honda/?geography=texas&geography=harris
How do I rewrite this url in wordpress to be:
xyz.com/dealers/honda/texas/harris/
you can do the following in your functions.php:
add_rewrite_rule('^dealers/honda/([^/]*)/([^/]*)/?','index.php?geography=$matches[1]&geography=$matches[2]','top');
However you should note that you have geography set twice and the last one is the one that will be used.
You will also need to navigate to your permalinks page at xyz.com/wp-admin/options-permalink.php before this actually works or else you'll get a 404ish error.

How to rewrite URI of custom post type?

The site that I am working on uses the following "pretty" permalink structure:
http://example.com/blog/my-special-post
But for a custom post type my client would like to avoid having a "pretty" slug:
http://example.com/product/142
How can the post ID be used in place of the slug?
I believe that this might be possible using WP_Rewrite, but I do not know where to begin.
I posted the same question on WordPress stack exchange site and received a good solution:
WordPress StackExchange - How to rewrite URI of custom post type?
All of WP's posts/pages, regardless of their type, can be reached via http://example.com/?p=foo, where foo is the post ID.
By default, redirection to a "pretty permalink" takes place though.
On the WP Codex entry for register_post_type() (I assume that's what you're using to set up your CPT, right?), the rewrite argument is described:
rewrite
(boolean or array) (optional) Rewrite permalinks with this format. False to prevent rewrite.
Default: true and use post type as slug
So, using
$args = array(
'rewrite' => false,
// ... whatever else there is
);
register_post_type('product', $args);
should yield what you're looking for.
In the WP admin you can chane the display of the permalinks by going to 'Settings->Permalinks'. To achieve your desired results, set the custom structure to /%post_id%/. Unless you manually edit .htaccess and the WP rewrite rules, I don't think you can achieve a structure where you have the post slug in the URI for one post type, but the ID for another.

Resources