So I'm in the position where I thought I had setup my custom post types custom url correctly (it should be in the format domain.com/malta-property/locationofproperty/referencenumber). I don't want the post name displaying on the end of the url (and it doesn't). the problem I am having is before I added the "$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');" code the urls were displaying the way I wanted but the page 404'd. when I added that code I thought that would fix it but now the properties just redirect to the home page? I have tried using "add_rewrite_tag" rather than $wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location='); but that didn't help in any way.

p.s. I am calling the functions in the filters/add_actions properly. that's not the issue.

 final public static function url_rewrite()
    // add_rewrite_tag('%ps_perry_pid%', '([^&]+)');
    // add_rewrite_tag('%ps_perry_slug%', '([^&]+)');
    // add_rewrite_rule('^malta-property/([^/]*)', 'index.php?ps_perry_pid=$matches[1]', 'top');
    //$wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%reference%', '([^&]+)', 'reference=');
    $wp_rewrite->add_rewrite_tag('%location%', '([^&]+)', 'location=');

    return null;

final public static function permalink_structure ($permalink, $post, $leavename)

  if (false !== strpos($permalink, '%reference%')) {
    $reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
    $location = wp_get_post_terms($post->ID, 'location');
    $department = wp_get_post_terms($post->ID, 'department');
    $rewritecode = array(

    $rewritereplace = array(
      $leavename? $post->post_name : '',

    $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
  return $permalink;

'rewrite'            => array( 'slug' => 'malta-property/%location%/%reference%' ),

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.