Home Forums Elementor Mobile Redirection Issue with Custom WordPress Elementor Widget Search Form

  • This topic has 0 replies, 1 voice, and was last updated 1 year ago by o3rge.
Viewing 0 reply threads
  • Author
    Posts
    • #32066
      o3rge
      Keymaster

      I’m currently working on a WordPress website and using Elementor as my page builder. I have a product search form that redirects users to an Elementor page using the “elementor_library” parameter included in the form as a hidden input. This works perfectly on a desktop, but when I’m on a mobile device, it redirects me to the homepage instead (even if I write directly url containing the request). I’m unsure why this is happening and I’d appreciate any help in resolving this issue.

      Here’s what my search form looks like:


      Search


      When the form is submitted, I’m redirected to the following URL: https://mywebsite.com/?search_products=userInput&elementor_library=elementor-archive-60215 “userInput” represents the value entered in the search field. As I mentioned earlier, this redirection works well on desktop but not on mobile devices.

      For reference, I have also included the following code in my child theme’s functions.php file in order to include categories and tags in the product search:

      add_action( ‘pre_get_posts’, ‘custom_search_products’ );
      function custom_search_products(WP_Query $query) {
      if (isset($_GET[‘search_products’]) && $query->is_search()) {
      $searchValue = sanitize_text_field($_GET[‘search_products’]);
      $query->set(‘post_type’, ‘product’);
      $query->set(‘s’, $searchValue);
      $query->set(‘search_prod_term’, $searchValue);
      }
      return $query;
      }

      function custom_search_where_clause($where, $wp_query) {
      global $wpdb;

      if ($search_term = $wp_query->get(‘search_prod_term’)) {
      $where .= ‘ OR (‘;
      $where .= ” EXISTS (SELECT * FROM {$wpdb->term_relationships} tr”;
      $where .= ” LEFT JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)”;
      $where .= ” LEFT JOIN {$wpdb->terms} t ON (t.term_id = tt.term_id)”;
      $where .= ” WHERE tt.taxonomy IN (‘product_cat’, ‘product_tag’)”;
      $where .= $wpdb->prepare(” AND t.name LIKE %s”, ‘%’ . $wpdb->esc_like($search_term) . ‘%’);
      $where .= $wpdb->prepare(” AND t.slug LIKE %s”, ‘%’ . $wpdb->esc_like($search_term) . ‘%’);
      $where .= ” AND {$wpdb->posts}.ID = tr.object_id”;
      $where .= ” AND {$wpdb->posts}.post_type IN (‘product’)”;
      $where .= ‘))’;
      }

      return $where;
      }
      add_filter(‘posts_where’, ‘custom_search_where_clause’, 10, 2);
      If anyone has encountered a similar issue or has any suggestions on how to fix this, please let me know. Your help would be greatly appreciated!

      Thank you!

Viewing 0 reply threads
  • You must be logged in to reply to this topic.
Close Search Window