Advertisement
mahfuz118

Directorist - Location Search Within the Search Bar

Jul 14th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. add_filter('atbdp_listing_search_query_argument', 'pfm_directorist_updated_args_for_searchbar');
  2.  
  3. function pfm_directorist_updated_args_for_searchbar($args)
  4. {
  5.     if (isset($_GET['q']) && !empty($_GET['q'])) :
  6.         $post_per_page = $args['posts_per_page'];
  7.         $args['posts_per_page'] = -1;
  8.         $args1 = $args;
  9.         $args1['fields'] = 'ids';
  10.         $post_args1 = new WP_Query($args1);
  11.         $posts1 = !empty($post_args1->have_posts()) ? $post_args1->posts : [];
  12.  
  13.         unset($args['s']);
  14.         $args2 = $args;
  15.         $args2['tax_query'] = array(
  16.             array(
  17.                 'taxonomy' => ATBDP_LOCATION,
  18.                 'field'    => 'name',
  19.                 'terms'    => explode(' ', $_GET['q']),
  20.                 'include_children' => 1
  21.             )
  22.         );
  23.         $args2['fields'] = 'ids';
  24.         $post_args2 = new WP_Query($args2);
  25.         $posts2 = !empty($post_args2->have_posts()) ? $post_args2->posts : [];
  26.         $all_ids = array_merge($posts2, $posts1);
  27.  
  28.         if (!empty($all_ids)) {
  29.             $args['post__in'] = $all_ids;
  30.         } else {
  31.             $args['s'] = $_GET['q'];
  32.         }
  33.         $args['posts_per_page'] = $post_per_page;
  34.     endif;
  35.  
  36.     return $args;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement