WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.