Yes, it’s possible, but think about what WebElaine wrote in comment.
You’ll need a query variable (e.g. jobname
) to store the name of jobs
custom post type.
You also need a rewrite rule that interprets /jobs/{post-slug}/contact
address as a “contact” page
and keep {post-slug}
in query variable jobname
.
add_filter( 'query_vars', 'se354723_query_vars' );
add_action( 'init', 'se354723_rewrite_job_contact' );
function se354723_query_vars( $vars )
{
$vars[] = "jobname";
return $vars;
}
function se354723_rewrite_job_contact()
{
$cpt_slug = 'jobs';
$target_page="contact-page-slug";
add_rewrite_rule(
"$cpt_slug/([^/]+)/contact/?$",
'index.php?pagename=". $target_page ."&post_type=page&jobname=$matches[1]',
'top'
);
}
You can check value of the new query variable (jobname
) on the “contact” page
with the get_query_var()
function.
$jobID = 0;
$jobname = get_query_var('jobname', false);
if ( !empty($jobname) )
{
$jobname = sanitize_title_for_query( $jobname );
$arg = [
'post_type' => 'jobs',
'name' => $jobname,
'fields' => 'ids',
'posts_per_page' => 1,
];
$result = get_posts( $arg );
if ( is_array($result) && !empty($result) )
$jobID = (int)$result[0];
}