Note: All code examples on this site are provided for developer reference/guidance only and we cannot guarantee that they will always work as expected. Our support policy does not include assistance with modifying or debugging code from any code examples, and they may be changed or removed if we find they no longer work due to changes in our plugins.
The following snippet demonstrates how to add custom field data to resume base permalinks. This is advanced!
<?php
/**
* Add rewrite tags for the data we want to insert and define the structure of the permalink.
*
* You need to go to Settings > Permalinks and save for these rules to be added.
*/
function add_resume_rewrite_rules(){
add_rewrite_tag( '%resume%', '([^/]+)', 'resume=' );
add_rewrite_tag( '%candidate_title%', '([^/]+)', 'candidate_title=' );
add_permastruct( 'resume', 'resumes/%candidate_title%/%resume%', true );
}
add_action( 'init', 'add_resume_rewrite_rules', 11 );
/**
* Add data to the permalinks
* @param string $permalink
* @param WP_Post $post
* @param bool
* @return string
*/
function resume_permalinks( $permalink, $post, $leavename ) {
// Only affect resumes
if ( $post->post_type !== 'resume' || empty( $permalink ) || in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
return $permalink;
}
// Get the meta data to add to the permalink
$title = get_post_meta( $post->ID, '_candidate_title', true );
// We need data, so fallback to this if its empty
if ( empty( $title ) ) {
$title = 'candidate';
}
// Do the replacement
$permalink = str_replace( '%candidate_title%', sanitize_title( $title ), $permalink );
return $permalink;
}
add_filter( 'post_type_link', 'resume_permalinks', 10, 3 );