Return to Snippet

Revision: 55768
at February 21, 2012 22:29 by nerdsane


Initial Code
add_action('init', 'post-type_rewrite');
function post-type_rewrite() {
  global $wp_rewrite;
  $queryarg = 'post_type=post-type&p=';
  $wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)', $queryarg);
  $wp_rewrite->add_permastruct('post-type', '/post-type/%post_id%', false);
  }

add_filter('post_type_link', 'post-type_permalink', 1, 3);
function post-type_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('bodumanzaru');
  $newlink = str_replace("%post_id%", $post->ID, $newlink);
  $newlink = home_url(user_trailingslashit($newlink));
  return $newlink;
  }

Initial URL


Initial Description
so you want to have your url structure like :
yourdomain.com/post-type/286

add the following snippet to your functions.php and change the post-type names where appropriate

Initial Title
Wordpress Custom Post Type : POST ID in post url

Initial Tags
post, wordpress

Initial Language
PHP