Title: custom post type with post_id in permalink structure
Last modified: August 19, 2016

---

# custom post type with post_id in permalink structure

 *  [ne0que](https://wordpress.org/support/users/ne0que/)
 * (@ne0que)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/)
 * Hi,
 * I tried to add
 *     ```
       array("slug" => "webspots/%post_id%")
       ```
   
 * for the rewrite attribute with creating a new post type. But %post_id% has not
   been replaced. I would like to add the post_id in the url, so when i change the
   title of the content, the link will not be broken in the future.
 * I’ve searched many placed on the web, but couldn’t find any explaination about
   these except 3 lines…
 * > One other way around this would be to use the add_permastruct function of the
   > $wp_rewrite object and manually define what kind of permalink structure you
   > would like to use for your custom type posts. Don’t forget to filter post_type_link
   > if you do.
 * on [this page](http://kovshenin.com/tag/customization/)
 * If someone can help me with this (with steps) how to create a link structure 
   like this:
 *     ```
       http://www.a-url.com/content-type-name/192130/title-of-content-type-item/
       ```
   

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/custom-post-type-permalink-structure/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/custom-post-type-permalink-structure/page/2/?output_format=md)

 *  Thread Starter [ne0que](https://wordpress.org/support/users/ne0que/)
 * (@ne0que)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526274)
 * Nobody has a clue?
 *  Thread Starter [ne0que](https://wordpress.org/support/users/ne0que/)
 * (@ne0que)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526350)
 *     ```
       add_filter('post_type_link', 'webspot_type_link', 1, 3);
       function webspot_type_link($post_link, $id = 0, $leavename = false)
           {
             if (strpos('%webspot_id%', $post_link) < 0)
             {
               return $post_link;
             }
             $post = get_post($id);
   
             if (!is_object($post) || $post->post_type != 'webspot')
             {
               return $post_link;
             }
             return str_replace('%webspot_id%', $post->ID, $post_link);
           }
       ```
   
 * Makes the link correctly. But when i press on ‘view’ i get redirected to a 404
   page. The URL is good:
 * `http://www.domain.com/webspots/47/vliegtuig-met-brandende-motor-maakt-noodlanding-
   schiphol/`
 * It gives me a 404 error.
 * What must i do more to let this work ?
 *  [hearsay_kev](https://wordpress.org/support/users/hearsay_kev/)
 * (@hearsay_kev)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526441)
 * I’m trying to do the very same thing but having no luck. I want a short and sweet
   URL for some pithy, custom post types. Just [http://example.tld/type/34](http://example.tld/type/34)
   would be awesome, but it doesn’t seem to be possible (or at least easily doable).
 *  [hearsay_kev](https://wordpress.org/support/users/hearsay_kev/)
 * (@hearsay_kev)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526443)
 * Looks like our answer is here:
 * [http://wordpress.org/support/topic/402661?replies=2](http://wordpress.org/support/topic/402661?replies=2)
 *  Thread Starter [ne0que](https://wordpress.org/support/users/ne0que/)
 * (@ne0que)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526457)
 * I do not understand why that link counts for us.
 * I’m not dealing with taxomonies. Just a a custom post type ;).
 * **When i go to **
    [http://www.example.com/webspots/18/](http://www.example.com/webspots/18/)
   _or_ [http://www.example.com/webspots/18/kijkers-roepen-wilders-uit-tot-winnaar-debat/](http://www.example.com/webspots/18/kijkers-roepen-wilders-uit-tot-winnaar-debat/)
 * **it should do this:**
    [http://www.example.com/index.php?p=18](http://www.example.com/index.php?p=18)
 * Nothing more, nothing less. Thats all.
 * then based on the content type it will be loaded in it’s own template.
 *  [Mark Rowatt Anderson](https://wordpress.org/support/users/markauk/)
 * (@markauk)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526549)
 * I’ve been trying to do something similar, but with post dates, such as:-
    `http://
   www.example.com/post_type/2010-06/post_title`
 * I’m coming to the conclusion that it might not be possible, as I’m not sure how
   WP would turn the URL above into a post ID without a lot of DB lookup.
 * I’d be very happy if someone proved me wrong, though 🙂
 * For your example, [@ne0que](https://wordpress.org/support/users/ne0que/) – could
   you not just use an apache mod_rewrite to turn the permalink into the index?p
   =ID format?
 *  [Mark Rowatt Anderson](https://wordpress.org/support/users/markauk/)
 * (@markauk)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526550)
 * Unfortunately, I’ve just answered my own question and mod_rewrite doesn’t seem
   to do the trick either.
 * Here’s what I got to work/not work:-
    in httpd.conf/.htaccess `RewriteRule ^themes?/([
   0-9]*)/.*$ index.php?p=$1 [L]` no rewrite parameter in register_post_type –> 
   this works (it goes to the right post) but WP rewrites the address to /post_type/
   post_name which rather defeats the purpose of doing this in the first place.
 * This also works with a simple rewrite parameter e.g. array(‘slug’=>’themes’)
 * However, as soon as I try to filter post_type_link to insert post ID, I end up
   with 404 post not found. For example:-
 *     ```
       function monthly_theme_permalink( $post_link, $id = 0, $leavename = FALSE ) {
   
       	if ( strpos($post_link,'%id%') === FALSE )
       		return $post_link;
   
       	$post = get_post($id);
       	if ( !is_object($post) || $post->post_type <> 'monthly_theme' )
       		return $post_link;
   
       	$post_link = preg_replace('|%id%.*|',$post->ID.'/',$post_link);
   
       	return $post_link;
       }
       add_filter('post_type_link', 'monthly_theme_permalink', 1, 3);
       ```
   
 *  [dpchen](https://wordpress.org/support/users/dpchen/)
 * (@dpchen)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526560)
 * I was having the same problem as well: couldn’t find a way to use post ID in 
   the permalink of my custom post type. Here’s what I did.
 * Add the rewrite rule:
 *     ```
       add_action('init', 'myposttype_rewrite');
       function myposttype_rewrite() {
       	global $wp_rewrite;
       	$queryarg = 'post_type=myposttype&p=';
       	$wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)', $queryarg);
       	$wp_rewrite->add_permastruct('myposttype', '/myposttype/%post_id%', false);
       	}
       ```
   
 * And hook the post_type_link filter:
 *     ```
       add_filter('post_type_link', 'myposttype_permalink', 1, 3);
       function myposttype_permalink($post_link, $id = 0, $leavename, $sample) {
       	global $wp_rewrite;
       	$post = &get_post($id);
       	if ( is_wp_error( $post ) )
       		return $post;
       	$newlink = $wp_rewrite->get_extra_permastruct('myposttype');
       	$newlink = str_replace("%post_id%", $post->ID, $newlink);
       	$newlink = home_url(user_trailingslashit($newlink));
       	return $newlink;
       	}
       ```
   
 * Remember to flush your rewrite rules before you view your custom post type using
   the new permalink. Simply visit your Permalink settings page in the admin panel.
 * I still recommend WordPress – in the future versions – to have better UI for 
   creating/managing custom post types, and allow a more flexible way of configuring
   custom post type permalinks.
 *  [dpchen](https://wordpress.org/support/users/dpchen/)
 * (@dpchen)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526561)
 * Just noticed that `%post_id%` overlaps with the built-in permalink placeholders,
   so your normal post permalinks might be corrupted if you are using post id’s 
   there as well. Simply change `%post_id%` to something original like `%cpt_id%`
   throughout your own functions and you’ll be fine.
 *  [joetraff](https://wordpress.org/support/users/joetraff/)
 * (@joetraff)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526576)
 * Fully agree with you dpchen.
    Your help is much appreciated.
 *  [:Dawn](https://wordpress.org/support/users/dawnhwilsonhotmailcom/)
 * (@dawnhwilsonhotmailcom)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526584)
 * I think I am having the same issues with my posts. I use it to display the WP
   Cumulus tag cloud and the links have not worked since I changed to a custom permalink
   structure.
 * Can anyone help me with this?
 * I really want to keep a nice URL for all of my pages, but I am not sure if that
   is possible. When I change the URL to the “default” option then it works fine.
 * Any suggestions by chance?
 * All my best,
    :Dawn
 * Link: [http://www.oliveralert.com/about-us](http://www.oliveralert.com/about-us)
 *  [dylanspurgin](https://wordpress.org/support/users/dylanspurgin/)
 * (@dylanspurgin)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526628)
 * Thanks for the great write-up, dpchen. Worked like a charm for me.
 * One edit: I had to remove the $sample variable from the second function definition.
   It looks like that function only takes 3 arguments now.
 *  [willroy](https://wordpress.org/support/users/willroy/)
 * (@willroy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526634)
 * dpchen, thanks for that, really useful..
 * How could I modify that so I could include the post’s slug AS WELL as id i.e:
 * `http://url.com/myposttype/this-is-a-custom-post-type-entry/12`
 * or
 * `http://url.com/myposttype/this-is-a-custom-post-type-entry-12`
 * or
 * `http://url.com/myposttype/12-this-is-a-custom-post-type-entry`
 * Whichever is easiest?
 * Thanks for any suggestions
 *  [willroy](https://wordpress.org/support/users/willroy/)
 * (@willroy)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526635)
 * I’ve got as far as displaying the links with the CPT entry name however it’s 
   returning a 404 not found when clicked on:
 * // Create a rewrite rule for the Vacancies CPT so that ID numbers can also be
   used:
 *     ```
       add_action('init', 'vacancies_rewrite');
       function vacancies_rewrite() {
         global $wp_rewrite;
         $queryarg = 'post_type=vacancies&p=';
         $wp_rewrite->add_rewrite_tag('%cpt_id%', '([^/]+)', $queryarg);
         $wp_rewrite->add_permastruct('vacancies', '/vacancies/%cpt_entry%/%cpt_id%', false);
       }
       ```
   
 * // Now hook the post_type_link filter
 *     ```
       add_filter('post_type_link', 'vacancies_permalink', 1, 3);
       function vacancies_permalink($post_link, $id = 0, $leavename) {
         global $wp_rewrite;
         $post = &get_post($id);
         if ( is_wp_error( $post ) )
           return $post;
         $newlink = $wp_rewrite->get_extra_permastruct('vacancies');
         $newlink = str_replace("%cpt_id%", $post->ID, $newlink);
         $newlink = str_replace("%cpt_entry%", $post->post_name, $newlink);
         $newlink = home_url(user_trailingslashit($newlink));
         return $newlink;
       }
       ```
   
 *  [rhysbwaller](https://wordpress.org/support/users/rhysbwaller/)
 * (@rhysbwaller)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/#post-1526640)
 * in regards to dpchen’s solution, what is the best way to restrict this rewrite
   to only one cpt? ie leaving all others at their default
 * Thanks

Viewing 15 replies - 1 through 15 (of 23 total)

1 [2](https://wordpress.org/support/topic/custom-post-type-permalink-structure/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/custom-post-type-permalink-structure/page/2/?output_format=md)

The topic ‘custom post type with post_id in permalink structure’ is closed to new
replies.

## Tags

 * [content-type](https://wordpress.org/support/topic-tag/content-type/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [Permalink structure](https://wordpress.org/support/topic-tag/permalink-structure/)

 * 23 replies
 * 12 participants
 * Last reply from: [odr_m9611](https://wordpress.org/support/users/odr_m9611/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/custom-post-type-permalink-structure/page/2/#post-1526663)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
