404 error using Meta for Permalink rewrite

I’m trying to rewrite my permalinks using meta data from my post. The permalink rewrite works fine but I’m getting the 404 error when I try to view the post.

Could someone offer a clue as to why this wouldn’t work?

add_action('init', 'new_rewrite');
function new_rewrite(){
   add_rewrite_tag("%mls%", '([a-zA-Z0-9])');
}

add_filter('post_link', 'mls_permalink', 10, 3);
add_filter('post_type_link', 'mls_permalink', 10, 3);

function mls_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%mls%') === FALSE) return $permalink;

    $post = get_post($post_id);
    if (!$post) return $permalink;

    $mls = get_post_meta($post->ID, 'mls_number', true );
    if (!is_wp_error($mls) && !empty($mls)) $mls_slug = $mls;
    else $mls_slug = '';

    return str_replace('%mls%', $mls_slug, $permalink);
}

Related posts

Leave a Reply