Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duogeekdev/d146d97e3c19008eb9b1 to your computer and use it in GitHub Desktop.
Save duogeekdev/d146d97e3c19008eb9b1 to your computer and use it in GitHub Desktop.
Append post ID at the end of the URL for a custom post type
<?php
function change_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'product' ){
return home_url( 'product/'. $post->post_name .'/'. $post->ID );
} else {
return $link;
}
}
add_filter('post_type_link', 'change_post_type_link', 10, 2);
add_action( 'init', 'change_rewrites_init' );
function change_rewrites_init(){
add_rewrite_rule(
'product/([a-z-]+)/([0-9]+)?$',
'index.php?post_type=product&p=$matches[1]&postid=$matches[2]',
'top' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment