O, I’ve just realised my mistake here. The ID doesn’t exist at that point!
We could perhaps use the base slug. That would make a URL look like this:
yoursite.com/job/ID/post_name
Here is the code for that:
function custom_job_post_type_link( $permalink, $post ) {
if ( $post->post_type !== 'job_listing' )
return $permalink;
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;
$find = array(
'%post_id%'
);
$replace = array(
$post->ID
);
$replace = array_map( 'sanitize_title', $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;
}
add_filter( 'post_type_link', 'custom_job_post_type_link', 10, 2 );
function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = _x( 'job', 'Job permalink - resave permalinks after changing this', 'wp-job-manager' ). '/%post_id%';
return $args;
}
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );