Saturday, October 16, 2021

Directorist | Tag Search Within Search Bar | Advance Search

Directorist | Tag Search Within Search Bar | Advance Search



Directorist search option is pretty advanced since they have introduced version 7 in March 2021. Now, users can create custom fields using the Directory Builder settings from the admin panel and use them on the search template to make the search function better than ever.

There are also some limitations in the search option, due to some reason to increase the speed of the website. Some users look for tag search within the search bar field, although they have the tag search option available on the search layout.

You can easily search tags within the search bar by simply following the steps mentioned below -

Copy and paste the following code to the "functions.php" file on the currently activated theme -



  1. add_filter('atbdp_listing_search_query_argument', 'directorist_search_with_tag_on_searchbar');
  2.  
  3. function directorist_search_with_tag_on_searchbar($args)
  4. {
  5.     if (!empty($_GET['q'])) {
  6.         $post_per_page = $args['posts_per_page'];
  7.         $args['posts_per_page'] = -1;
  8.         $has_posts = new WP_Query($args);
  9.         $post_ids = [];
  10.  
  11.         if ($has_posts->have_posts()) {
  12.             foreach ($has_posts->posts as $post) {
  13.                 array_push($post_ids, $post->ID);
  14.             }
  15.         }
  16.  
  17.         unset($args['s']);
  18.  
  19.         $args2 = $args;
  20.  
  21.         $tax_queries = array(
  22.             'taxonomy' => ATBDP_TAGS,
  23.             'field' => 'name',
  24.             'terms' => explode(' ', $_GET['q']),
  25.         );
  26.  
  27.         $args2['tax_query'] = array(
  28.             'relation' => 'AND',
  29.             $tax_queries
  30.         );
  31.  
  32.         $args2['fields'] = 'ids';
  33.  
  34.         $post_args2 = new WP_Query($args2);
  35.  
  36.         $posts = !empty($post_args2->have_posts()) ? $post_args2->posts : [];
  37.  
  38.         $all_ids = array_merge($post_ids, $posts);
  39.  
  40.         $args['post__in'] = !empty($all_ids) ? $all_ids : array();
  41.  
  42.         $args['posts_per_page'] = $post_per_page;
  43.     }
  44.     return $args;
  45. }



This should work like charm. 

You can comment below if you need have any questions regarding this.

Enjoy Directorist.

No comments:

Post a Comment