How to rewrite URI of custom post type? - wordpress

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.

Related

How to change custom WordPress permalinks?

I have a custom theme whereas the format of custom post is http://www.example.com/coupon/store-name/title-name
Is there a way to overwrite in whole site pages and change to http://www.example.com/deals/title-name
I tried Permalinks and Custom Post Type Permalinks and it didn't work.
A custom post type is registered with the register_post_type function and its URL slug is set with its rewrite arg. The good news is that you can use the register_post_type function to modify an existing post type.
You are going to need to:
get the args which were used to register this post type. You can use the get_post_type_object function for this
modify these as needed, that is, add your rewrite rules
call register_post_type with modified args

Wordpress "custom post type permalinks" plugin generates 404 Page not found

I need the links of all personalized posts and taxonomies to be structured with the name of the category (s) they belong to.
Unfortunately the theme used for my site generates a new type of post and the new taxonomy programmatically and performs the rewrite of their permalink.
To solve this problem I configured the "Custom Post Type Permalinks" plugin which works, but only halfway:
- the permalinks of taxonomies are generated correctly and are accessible from the site.
- the permalinks of the posts are generated correctly, but are not accessible from the site and return a page with a "404 Page not found" error.
In my httpd.conf the line LoadModule rewrite_module modules/mod_rewrite.so is not commented.
What can I do?
let me give you a deeper idea about what we’d like to get. We would really appreciate if you could help us reaching it, by letting us know where to operate.
We would like all the tours’ url to be made up like this: www.sitename.com/$category_father/$category_child/$name_of_my_post Moreover, we would like all the tours’ taxonomies url to be made up like this: www.sitename.com/$category_father/$category_child
At the moment your Tourmaster plugin creates the taxonomies and the tours as below:
www.sitename.com/tour/$name_of_my_post
www.sitename.com/tour-category/$category_child
In order to avoid editing the plugin code and prevent any updates conflicts we installed the plugin “Custom Post Type Permalinks”. It partially solves the taxonomies problem and partially the tours one, generating almost correct urls which are unfortunately not reachable.
www.sitename.com/tour/$category-father/$category_child/$name_of_my_post
www.sitename.com/tour/tour-category/ $category_father/$category_child
Referring to the readme.txt file within www.sitename.com/wp-content/plugins/custom-post-type-permalinks/ we could notice the following example code.
register_post_type( 'foo',
array(
"public" => true,
'has_archive' => true,
"rewrite" => ["with_front" => true],
"cptp_permalink_structure" => "%post_id%"
) );
So we edited the Tourmaster plugin code replacing the rewrite array within the file www.sitename.com/wp-content/plugins/tourmaster/include/tour-option.php with the array [“with_front” => true], but we did not get any result. When you saw the url working, it was due to a temporary reset of the CPTP settings.

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

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.

wordpress - Issue With custom post types and permalinks

I'm working with Custom Post Types for the first time and running into a vexing problem:
I can add and query the post types, but they always show up as:
domain.com/post-type-name/post-title
I have no rewrite in place, and my permalink structure is set to %postname% only, and I am flushing the rules immediately following adding the post type.
Any ideas on what I'm doing wrong?
This could be helpful to solve the problem:
http://www.ultimatewebtips.com/remove-slug-from-custom-post-type/
You may try two things
Just visit your Permalink Settings from the settings menu and save the page again.
or you may try
register_post_type('post-type-name', array(
//...
'rewrite' => array('slug'=>'','with_front'=>false),
//...
));

Resources