IF you want to rewrite custom post type url in WordPress? This is what I use to rewrite custom post type URLs with the post ID. You need a rewrite rule to translate URL requests, as well as a filter on post_type_link to return the correct URLs for any calls to get_post_permalink()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3); function wpse33551_post_type_link( $link, $post = 0 ){ if ( $post->post_type == 'product' ){ return home_url( 'product/' . $post->ID ); } else { return $link; } } add_action( 'init', 'wpse33551_rewrites_init' ); function wpse33551_rewrites_init(){ add_rewrite_rule( 'product/([0-9]+)?$', 'index.php?post_type=product&p=$matches[1]', 'top' ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.