Support » Plugins » 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:

    http://www.a-url.com/content-type-name/192130/title-of-content-type-item/
Viewing 8 replies - 16 through 23 (of 23 total)
  • @rhysbwaller
    You can set the condition in the function
    if (post_type is……………..

    Thanks for the reply Weblogian. The code I used is here:

    if(get_post_type() == 'post-type-name'){
    …
    		return $newlink;
    	} else {
    		return $post_link;
    	}

    Thanks

    Hi

    I want to change the permalink structure like this
    http://Example.com/ads/748
    http://Example.com/ads/%cpt_id%
    ======
    I used this:

    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', '/ads/%cpt_id%', false);
    }
    
    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 = home_url(user_trailingslashit($newlink));
    
      return $newlink;
    
    }

    like in above ,the permalink will changed,but when click and go to the that link,the page 404 [Not Found] is shown .

    Where of this code should be change?
    Please help me for this

    Thanks

    I’ve come up with a solution to this, finally!

    Here’s how I’m using it:

    Permalink structure =
    / Custom Post Type Slug / Post ID / Post Name Slug /
    aka
    http://iloveomfg.com/tracks/%post_id%/%postname%/
    which outputs
    http://iloveomfg.com/tracks/377/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/

    —–
    The trick was to replace the string %cpt_entry% with the already built-in WP permalink string %postname%. Doing this eliminates the 404/Page Not Found errors altogether, and also allows you to edit the post slug in the post editor like you can normally.

    Here is the final code which I used, based on dpchen’s earlier post in this thread.

    Add the following to functions.php:

    add_action('init', 'tracks_rewrite');
    function tracks_rewrite() {
      global $wp_rewrite;
      $queryarg = 'post_type=tracks&p=';
      $wp_rewrite->add_rewrite_tag('%cpt_id%', '([^/]+)', $queryarg);
      $wp_rewrite->add_permastruct('tracks', '/tracks/%cpt_id%/%postname%/', false);
    }
    
    add_filter('post_type_link', 'tracks_permalink', 1, 3);
    function tracks_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('tracks');
      $newlink = str_replace("%cpt_id%", $post->ID, $newlink);
      $newlink = home_url(user_trailingslashit($newlink));
      return $newlink;
    }

    The great thing about using this type of permalink structure, and the specific reason I am doing it is because it allows you to make easy short-links of your urls… for instance:
    http://iloveomfg.com/tracks/377/
    works the same as
    http://iloveomfg.com/tracks/377/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/

    However, for some reason, this does not work:
    http://iloveomfg.com/tracks/flux-pavilion-bass-cannon-flufftronix-nolibs-club-edit/
    For me, it displays an archive of all my custom post type “tracks”.

    Hope that helps you guys out!

    Actually, I am still getting some errors.. for instance the php get_permalink is pulling them like this:
    http://iloveomfg.com/tracks/377/%postname%/

    This totally makes no sense. I think it is because I messed with the code. Damn.

    Thanks a lot
    Can you help me for change my url like this?

    I want to change the permalink structure like this
    http://Example.com/ads/748
    http://Example.com/ads/%cpt_id%

    I don’t know how to change your code for my structure

    Thanks again

    odr_m9611, the following code should work for you:

    add_action('init', 'ads_rewrite');
    function ads_rewrite() {
      global $wp_rewrite;
      $queryarg = 'post_type=ads&p=';
      $wp_rewrite->add_rewrite_tag('%cpt_id%', '([^/]+)', $queryarg);
      $wp_rewrite->add_permastruct('ads', '/ads/%cpt_id%/', false);
    }
    
    add_filter('post_type_link', 'ads_permalink', 1, 3);
    function ads_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('ads');
      $newlink = str_replace("%cpt_id%", $post->ID, $newlink);
      $newlink = home_url(user_trailingslashit($newlink));
      return $newlink;
    }

    Let me know if it works!

    Thanks kingcrunk

    I’m add it to the function.php

    url changed,but when click the linkes ,go to the Not Found page

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