Lea Hayes
Lea Hayes

Reputation: 64236

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.

Upvotes: 4

Views: 4689

Answers (3)

Lea Hayes
Lea Hayes

Reputation: 64236

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?

Upvotes: 1

David Gard
David Gard

Reputation: 12097

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.

Upvotes: 0

vzwick
vzwick

Reputation: 11064

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.

Upvotes: 0

Related Questions