Adding Post id to the end of slug in a Custom Post Type
I have created a Custom Post Type (jobs) – Functions fine. But due to the possibility of duplicate page titles by admin, I need the permalink/slug to include the Post_ID to the end of the Post title, to make it unique, not a new directory but add -id to page title. I have investigated several options. This seems to look the cleanest and simplest addition to my standard post type registration code.
The below code has been added to my Post type registration code, makes the frontend links correct www.domain.com/jobs/postname-postid (www.domain.com/jobs/**accountant**-**123**). However, the rewrite element after, is not working as I get a 404. I’m also concerned about any negative/good practice impact of this on other functions/seo. But it does look quite standard and security safe. Help greatly appreciated.
function mycustomname_links($post_link, $post = 0) {
if($post->post_type === 'listings') {
return home_url('jobs/' .$post->post_name . '-' .$post->ID);
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'mycustomname_links', 1, 3);
Rewrite element
function mycustomname_rewrites_init(){
add_rewrite_rule('jobs/([0-9]+)?$', 'index.php?post_type=listings&p=$matches[1]', 'top');
}
add_action('init', 'mycustomname_rewrites_init');
Full Code
//callback
function lt_register_listing_type() {
// Labels array - where labels go.
$labels = array(
'name' => __( 'Job Listings', LTDOMAIN ),
'singular_name' => __( 'Job Listing', LTDOMAIN ),
'featured_image' => __( 'Recruiter Logo', LTDOMAIN ),
'set_featured_image' => __( 'Set Recruiter Logo', LTDOMAIN ),
'remove_featured_image' => __( 'Remove Recruiter Logo', LTDOMAIN ),
'use_featured_image' => __( 'Use Recruiter Logo', LTDOMAIN ),
'archives' => __( 'Job Archive', LTDOMAIN ),
'add_new' => __( 'Add New Job Listing', LTDOMAIN ),
'add_new_item' => __( 'Add New Job Item', LTDOMAIN ),
'edit_item' => __( 'Edit Job Listing', LTDOMAIN ),
);
// arguments array
$args = array(
'label' => __( 'Job Listings', LTDOMAIN ),
'labels' => $labels,
'menu_icon' => 'dashicons-media-document',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
// 'rewrite' => array( 'has_front' => true ),
'rewrite' => array( 'slug' => 'jobs' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'supports' => [ 'title', 'editor', 'trackbacks', 'thumbnail', 'excerpt', 'custom-fields', 'comments', 'revisions', 'author', 'page-attributes', 'post-formats' ],
'taxonomies' => [ 'category', 'post_tag' ],
'show_in_rest' => true,
);
register_post_type( 'listings', $args );
}
add_action( 'init', 'lt_register_listing_type' );
//Front end link change
function mycustomname_links($post_link, $post = 0) {
if($post->post_type === 'listings') {
return home_url('jobs/' .$post->post_name . '-' .$post->ID);
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'mycustomname_links', 1, 3);
//Rewrite
function mycustomname_rewrites_init(){
add_rewrite_rule('jobs/([0-9]+)?$', 'index.php?post_type=listings&p=$matches[1]', 'top');
}
add_action('init', 'mycustomname_rewrites_init');
Leave an answer