0

I got a previous question answered and I tried amending the solution, but now the problem is when there is NOT a space in my custom post URL the tag at position /location/ (in domain.com/malta-property/department/location/reference_id) - originally it was just /malta-property/location/reference_id) but now I've added the /department/ then location will just 404 unless there is a space in the name. It's odd. Note: trans_type means departments

The potentially interesting bits are the regex maybe? But I can't see anything wrong with them: just for specificity the regex segments are:

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

and

if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {

the code that builds the custom url for properties is this:

(the register post type args):

final private static function register_post_types ()
{
    $labels = array(
        'name'               => _x( 'Properties', 'post type general name', ' propertystreambootstrap' ),
        'singular_name'      => _x( 'Property', 'post type singular name', ' propertystreambootstrap' ),
        'menu_name'          => _x( 'Properties', 'admin menu', ' propertystreambootstrap' ),
        'name_admin_bar'     => _x( 'Property', 'add new on admin bar', ' propertystreambootstrap' ),
        'add_new'            => _x( 'Add New', 'property', ' propertystreambootstrap' ),
        'add_new_item'       => __( 'Add New Property', ' propertystreambootstrap' ),
        'new_item'           => __( 'New Property', ' propertystreambootstrap' ),
        'edit_item'          => __( 'Edit Property', ' propertystreambootstrap' ),
        'view_item'          => __( 'View Property', ' propertystreambootstrap' ),
        'all_items'          => __( 'All Properties', ' propertystreambootstrap' ),
        'search_items'       => __( 'Search Properties', ' propertystreambootstrap' ),
        'parent_item_colon'  => __( 'Parent Properties:', ' propertystreambootstrap' ),
        'not_found'          => __( 'No properties found.', ' propertystreambootstrap' ),
        'not_found_in_trash' => __( 'No properties found in Trash.', ' propertystreambootstrap' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Properties for your website.', ' propertystreambootstrap' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_icon'          => 'dashicons-building',
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'malta-property/%trans_type%/%location%/%reference%' ),
    //'rewrite'            => array( 'slug' => 'malta-property'),
    'has_archive' => 'about-cool-post-types',
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title', 'editor', 'excerpt', 'post-thumbnails', 'thumbnail')
    );
    register_post_type( 'property', $args );

url rewrite that gets called on init and I have flushed permalinks:

final public static function url_rewrite()
{
    global $wp_rewrite;
    $permastruct = $wp_rewrite->extra_permastructs['property'];
    $permastruct['struct'] = str_replace( '/%property%', '', $permastruct['struct'] );
    $wp_rewrite->extra_permastructs['property'] = $permastruct;
    $wp_rewrite->add_rewrite_tag('%reference%', '([^/]+)', 'reference=');
    $wp_rewrite->add_rewrite_tag('%trans_type%', '([^/]+)', 'trans_type=');
    $wp_rewrite->add_rewrite_tag('%location%', '([^/]+)', 'location=');
    return null;
}

get the product via the URL:

final public static function get_prod_by_ref( $wp )
{
    // Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
    if ( preg_match( '#^malta-property/([^/]+)/([^/]+)/([^/]+)#', $wp->request, $matches ) ) {
        $posts = get_posts( [
            'post_type'      => 'property',
            'meta_key'       => 'propertystream_agent_ref',
            'meta_value'     => $matches[3],
            'posts_per_page' => 1,
        ] );

        if ( ! empty( $posts ) ) {

            $wp->query_vars['name'] = $posts[0]->post_name;
            $wp->query_vars['post_type'] = 'property'; // must set post type
        }
    }
}

build the custom permalink structure (filtered to post_type_link):

final public static function permalink_structure ($permalink, $post, $leavename)
{
    if (false !== strpos($permalink, '%reference%') && false !== strpos($permalink, '%location%') && false !== strpos($permalink, '%trans_type%')) {
      $reference = get_post_meta($post->ID, 'propertystream_agent_ref', true);
      $location = wp_get_post_terms($post->ID, 'location');
      $type = wp_get_post_terms($post->ID, 'trans_type_id');
      //check trans_type_id is set, it it is and is resale the slug should be sales. otherwise just assign it the value
      if (!empty($type)) {
        if ($type[0]->slug == 'resale') {
          $type = 'sales';
        }
        else
        {
          $type = $type[0]->slug;
        }
      }
      else {
        $type = null;
      }

      $location = !empty($location) ? $location[0]->name : null;
      $rewritecode = array(
        '%reference%',
        '%location%',
        '%trans_type%'
      );
      $rewritereplace = array(
        $reference,
        $location,
        $type,
      );
      $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    return $permalink;
  }
1

So after discussing via chat, the problem is with Ps_Perry_Import_To_Post::wp():

final public static function wp()
{
    ...
    if(!empty($_GET['pid']) && empty($_GET['r'])) {
      ...
      wp_redirect('/malta-property/'.$_GET['pid'].'/'.$post_name.'?r=1');
      exit();
    }
    if(strpos($_SERVER['REQUEST_URI'], '/malta-property/') !== false && empty($_GET['r'])) {
      ...
      if (preg_match('/[A-Za-z]/', $arr2[count($arr2)-1]) && preg_match('/[0-9]/', $arr2[count($arr2)-1])) {
        // do nothing
        return null;
      } else {
        wp_redirect('/?pid='.$arr1[2]);
        exit();
      }
    }
    return null;
}

It's hooked to wp like so:

add_action('wp', ['Ps_Perry_Import_To_Post', 'wp'], 9);

So you should just remove that (and the above function) and if you need to redirect to the "property" based on the $_GET['pid'], then try this:

final public static function get_prod_by_ref( $wp )
{
    if ( ! empty( $_GET['pid'] ) ) {
        $reference = $_GET['pid'];
        $redirect = true;
    }

    // Check if the request/page path begins with /malta-property/<trans_type>/<location>/<reference>
    // If yes, cancel above redirect and display the single "property" page.
    if ( preg_match( '#^malta-property/([^/]*)/([^/]*)/([^/]+)#', $wp->request, $matches ) ) {
        $reference = $matches[3];
        $redirect = false;
    }

    if ( ! empty( $reference ) ) {
        $posts = get_posts( [
            'post_type'      => 'property',
            'meta_key'       => 'propertystream_agent_ref',
            'meta_value'     => $reference,
            'posts_per_page' => 1,
        ] );

        if ( ! empty( $posts ) ) {
            if ( $redirect ) {
                wp_redirect( get_permalink( $posts[0] ) );
                exit;
            }

            $wp->query_vars['name'] = $posts[0]->post_name;
            $wp->query_vars['post_type'] = 'property'; // must set post type
        }
    }
}
  • PS: Is that trans_type_id a typo - $type = wp_get_post_terms($post->ID, 'trans_type_id'); ? Perhaps you meant trans_type? – Sally CJ Jul 23 at 10:43
  • No trans_type_id is pulling the right stuff in! i just called the variable trans type from then on for simplicity, I'll test this problem now! – seanyt123 Jul 23 at 10:47
  • No it appears that this isn't the issue. changing this does not stop page only working if there is a space in the /location/ segment – seanyt123 Jul 23 at 11:01
  • I've added an update to the answer.. – Sally CJ Jul 23 at 11:11
  • This still hasn't fixed it :/ I fail to see how it being conditional would be the problem anyway because while conditional they are always set. so location is set in all instances. for example lets say the location is the united kingdom, the problem is this: local.domain.com/malta-property/sales/united kingdom/reference123/ works absolutely fine and shows the post but this: local.domain.com/malta-property/sales/unitedkingdom/… does not work and redirects to the homepage. (it's worth mentioning that a space in the url is really just %20) – seanyt123 Jul 23 at 11:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.