Support » Plugins and Hacks » custom post type with post_id in permalink structure

custom post type with post_id in 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

    If someone can help me with this (with steps) how to create a link structure like this:

Viewing 15 replies - 1 through 15 (of 23 total)
  • Nobody has a clue?

    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:


    It gives me a 404 error.

    What must i do more to let this work ?

    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 would be awesome, but it doesn’t seem to be possible (or at least easily doable).

    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

    it should do this:

    Nothing more, nothing less. Thats all.

    then based on the content type it will be loaded in it’s own template.

    I’ve been trying to do something similar, but with post dates, such as:-

    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 – could you not just use an apache mod_rewrite to turn the permalink into the index?p=ID format?

    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);

    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.

    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.

    Fully agree with you dpchen.
    Your help is much appreciated.



    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,

    Link: http://www.oliveralert.com/about-us

    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.

    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:






    Whichever is easiest?

    Thanks for any suggestions

    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;

    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


Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘custom post type with post_id in permalink structure’ is closed to new replies.