WordPress.org

Ready to get started?Download WordPress

Forums

custom post type with post_id in permalink structure (24 posts)

  1. Ne0Que
    Member
    Posted 3 years ago #

    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/
  2. Ne0Que
    Member
    Posted 3 years ago #

    Nobody has a clue?

  3. Ne0Que
    Member
    Posted 3 years ago #

    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 ?

  4. hearsay_kev
    Member
    Posted 3 years ago #

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

  5. hearsay_kev
    Member
    Posted 3 years ago #

  6. Ne0Que
    Member
    Posted 3 years ago #

    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/
    or
    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

    Nothing more, nothing less. Thats all.

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

  7. Mark Rowatt Anderson
    Member
    Posted 3 years ago #

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

  8. Mark Rowatt Anderson
    Member
    Posted 3 years ago #

    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);
  9. dpchen
    Member
    Posted 3 years ago #

    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.

  10. dpchen
    Member
    Posted 3 years ago #

    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.

  11. joetraff
    Member
    Posted 3 years ago #

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

  12. :Dawn
    Member
    Posted 3 years ago #

    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

  13. dylanspurgin
    Member
    Posted 3 years ago #

    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.

  14. willroy
    Member
    Posted 3 years ago #

    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

  15. willroy
    Member
    Posted 3 years ago #

    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;
    }
  16. rhysbwaller
    Member
    Posted 3 years ago #

    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

  17. Marc P Gangmei
    Member
    Posted 3 years ago #

    @rhysbwaller
    You can set the condition in the function
    if (post_type is.................

  18. rhysbwaller
    Member
    Posted 3 years ago #

    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

  19. odr_m9611
    Member
    Posted 2 years ago #

    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

  20. kingcrunk
    Member
    Posted 2 years ago #

    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!

  21. kingcrunk
    Member
    Posted 2 years ago #

    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.

  22. odr_m9611
    Member
    Posted 2 years ago #

    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

  23. kingcrunk
    Member
    Posted 2 years ago #

    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!

  24. odr_m9611
    Member
    Posted 2 years ago #

    Thanks kingcrunk

    I'm add it to the function.php

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

Topic Closed

This topic has been closed to new replies.

About this Topic