WordPress 404 unless there is a space in url

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']."https://wordpress.stackexchange.com/".$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
        }
    }
}

Leave a Comment