Skip to content

Instantly share code, notes, and snippets.

@atomtigerzoo
Created July 24, 2015 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomtigerzoo/d21bb4d487c41d7f68ea to your computer and use it in GitHub Desktop.
Save atomtigerzoo/d21bb4d487c41d7f68ea to your computer and use it in GitHub Desktop.
Wordpress: Custom post type with post_id-only in URLs
/**
* Replace 'mycustomname' with the actual custom type post
* name to fit your needs.
*
* !! Flush rewrite rules in Wordpress backend afterwards !!
*/
/**
* Create the CTP
*/
function create_post_type_mycustomname() {
$args = array(
'capability_type' => 'post',
'has_archive' => 'mycustomname',
'rewrite' => array(
'slug' => '/mycustomname',
'feeds' => false
)
);
register_post_type('ctp_mycustomname', $args);
}
add_action('init', 'create_post_type_mycustomname');
/**
* Correct post links for CTP
*/
function mycustomname_links($post_link, $post = 0) {
if($post->post_type === 'ctp_mycustomname') {
return home_url('mycustomname/' . $post->ID . '/');
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'mycustomname_links', 1, 3);
/**
* Rewrite rules for the CTP
*/
function mycustomname_rewrites_init(){
add_rewrite_rule('mycustomname/([0-9]+)?$', 'index.php?post_type=ctp_mycustomname&p=$matches[1]', 'top');
}
add_action('init', 'mycustomname_rewrites_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment