Home Redirect custom taxonomy with query var
Reply: 0

Redirect custom taxonomy with query var

user1903
1#
user1903 Published in May 25, 2018, 11:12 am

I am trying to redirect so when a url is: /location/location-name/buy it goes to a different template to /location/location-name/

here is what I have now (have tried various so this is latest):

   public static function add_query_vars() {
    global $wp;
    $vars[] = 'rent';
    $vars[] = 'buy';

     return $vars;
  }
    add_filter( 'query_vars', array( __CLASS__, 'add_query_vars' ) );
  /**
   * Add rewrite rules
   */
  public static function add_rewrite_rules() {

  //  add_rewrite_rule( '^location/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$', 'index.php?post_type=property&_property_locations=$matches[1]&API_property_type=$matches[2]', 'top' );
  add_rewrite_rule( 'location/([^/]+)/buy', 'index.php?post_type=property&_property_locations_=$matches[1]&buy=yes', 'top' );
  add_rewrite_rule( 'location/([^/]+)/rent', 'index.php?_property_locations_=$matches[1]&rent=yes', 'top' );

  flush_rewrite_rules();
  }
    add_action( 'init', array( __CLASS__, 'add_rewrite_rules' ) );

  public static function prefix_url_rewrite_templates() {

      if ( get_query_var( 'rent' ) ) {
        $base_name = 'includes/plugins/properties/templates/template-property-type.php';
        $template = locate_template( $base_name );
        if ( $template && !empty( $template ) ) return $template;
      }

      if ( get_query_var( 'buy' ) ) {
        $base_name = 'includes/plugins/properties/templates/template-property-type.php';
        $template = locate_template( $base_name );
        if ( $template && !empty( $template ) ) return $template;
      }
  }
    add_action( 'template_redirect', array( __CLASS__, 'prefix_url_rewrite_templates') );

Obviously doing something wrong probably to do with the rewrite on the taxonomy or the hooks I am using so any help would be greatly appreciated.

Thanks, Max

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341215 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO