• Resolved portugaldirectory

    (@portugaldirectory)


    Hi all,

    Fellow Directorist users, could you please share how you overcame the challenge of searching for businesses based on their tags? Currently, exact tag matches are required for successful searches. For instance, if a company has the tag “Cats,” searching for “cat/cats/Cat” will not yield results, only the exact match “Cats” will work.

    Thanks

    Clint

Viewing 1 replies (of 1 total)
  • Plugin Support Mahfuzul Alam

    (@mahfuz87)

    Hi

    You can use this custom code in the functions.php file of the active theme folder, its better to use a child theme for this.

    /**
     * Search with taxonomy
     */
     
    add_filter('atbdp_listing_search_query_argument', 'new_custom_directorist_updated_args_for_searchbar');
     
     
    function new_custom_directorist_updated_args_for_searchbar($args)
    {
        if (isset($_GET['q']) && !empty($_GET['q'])) :
            $post_per_page = $args['posts_per_page'];
            $args['posts_per_page'] = -1;
            $args1 = $args;
            $args1['fields'] = 'ids';
            $post_args1 = new WP_Query($args1);
            $posts1 = !empty($post_args1->have_posts()) ? $post_args1->posts : [];
            unset($args['s']);
            $args2 = $args;
            $catIds = get_terms([
                'name__like' => $_GET['q'],
                'fields' => 'ids',
                'taxonomy' => ATBDP_CATEGORY,
                'hide_empty' => false
            ]);
     
            $locIds = get_terms([
                'name__like' => $_GET['q'],
                'fields' => 'ids',
                'taxonomy' => ATBDP_LOCATION,
                'hide_empty' => false
            ]);
     
            $tagIds = get_terms([
                'name__like' => $_GET['q'],
                'fields' => 'ids',
                'taxonomy' => ATBDP_TAGS,
                'hide_empty' => false
            ]);
     
            $args2['tax_query'] = array(
                'relation' => 'OR',
                array(
                    'taxonomy' => ATBDP_CATEGORY,
                    'field'    => 'id',
                    'terms'    => $catIds,
                    'include_children' => 1
                ),
                array(
                    'taxonomy' => ATBDP_LOCATION,
                    'field'    => 'id',
                    'terms'    => $locIds,
                    'include_children' => 1
                ),
     
                array(
                    'taxonomy' => ATBDP_TAGS,
                    'field'    => 'id',
                    'terms'    => $tagIds,
                ),
            );
     
            $args2['fields'] = 'ids';
            $post_args2 = new WP_Query($args2);
            $posts2 = !empty($post_args2->have_posts()) ? $post_args2->posts : [];
            $all_ids = array_merge($posts2, $posts1);
     
            if (!empty($all_ids)) {
                $args['post__in'] = $all_ids;
            } else {
                $args['s'] = $_GET['q'];
            }
     
            $args['posts_per_page'] = $post_per_page;
        endif;
        return $args;
    }

    Thank you

Viewing 1 replies (of 1 total)
  • The topic ‘Search’ is closed to new replies.