Wordpress custom post type in wrong taxonomies - wordpress

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.

Related

.htaccess: Rewite rule to change to Semantic URL Struture

I am trying to change the URL structure of a search filter using .htaccess from this:
/tours/?tour-type=guided&tour-destination=europe&tour-type=guided&tour-duration=17%2B&tour-season=2020
to this:
/tours/guided/europe/guided/17/2020
Obviously the search filter has many different options such as days, locations different types and the like, I know it's possible in .htaccess but its proving elusive to me.
In wordpress it's better to use add_rewite_rule function. In example below I assume that tours is custom post type so post_type=tours. You can put there page id, category slug or whatever you want.
Note that with this rule if you want to send only tour-season you have to send something for rest of variables.
function tour_add_rewrite_rules() {
add_rewrite_rule('^tours/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$', 'index.php?post_type=tours&tour-type=$matches[1]&tour-destination=$matches[2]&tour-type=$matches[3]&tour-duration=$matches[4]&tour-season=$matches[5]', 'top' );
}
add_action('init', 'tour_add_rewrite_rules', 10, 0);

WordPress post permalinks for multiple categories, tags and taxonomy terms

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=.

Remove 'custom-cpt' from 'custom-cpt/post-title' from custom post types permalinks

Lets say I have this custom post type: 'pt-services'
So, posts will have this kind of urls:
pt-services/post-one
pt-services/post-two
How is it posible to remove pt-services from the urls? So they look like:
/post-one
/post-two
I tried what suggested in here but didn't work...
Any thoughts?

Tags create/edit/delete hooks in wordpress

I need hooks if tags have removed, created and edited. I have not found them in the WordPress Codex / Function Reference.
For categories:
edit_category
create_category
delete_category
Has anyone encountered this problem?
To explicitly target tags, you'll need create_$taxonomy, edit_$taxonomy and delete_$taxonomy where $taxonomy is post_tag (ie create_post_tag, edit_post_tag and delete_post_tag). They're mentioned in passing in (eg) the wp_insert_term.
Tags are a taxonomy, so the generic actions create_term, edit_term, and delete_term would work as well, though they'll also fire for other taxonomies (such as categories).
You need to use dynamic hooks to taxonomies:
edit_post_tag
create_post_tag
delete_post_tag

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