I have a custom post type named patch and custom permalink structure:


I managed to replace %version_id with actual version code with this code:

add_action('init', 'patch_rewrite_rules');
add_filter('post_type_link', 'patch_permalinks', 10, 3);

function patch_rewrite_rules() {

    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag('%version_id%', '([^/]+)', 'version_id=');

    $wp_rewrite->add_permastruct('patch', '/patch/%version_id%', false);


function patch_permalinks($permalink, $post, $leavename) {

    $post_id = $post->ID;

    if($post->post_type != 'patch' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;

    $version_id = get_field('version_id', $post_id);

    $permalink = str_replace('%version_id%', $version_id, $permalink);

    return $permalink;


So now I have links with proper URL, for example:


But all of them lead to site main page!

How to make this link lead to specific patch (version_id is an unique meta property)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.