How do I remove a specific Topic (category) from the WordPress plug-in FAQ manager from the search?

Asked 1 years ago, Updated 1 years ago, 92 views

"We put the plug-in ""FAQ manager"" in wordPress to build the FAQ page."
FAQs include Topic (category) for members and
when searching for FAQs when members are not logged in. Remove the Topic (category) for members from the search and
the category when logging in. We are considering implementing it as a search target.

If you google the wordPress functions.php and implement the following
I tried implementing certain categories because I knew they would be out of search.

function SearchFilter($query){
    if(!is_admin()&$query->is_main_query()&$query->is_search(){
        $query->set('category__not_in', array(1,2));// Specify Category ID
    }
    return$query;
}
add_filter('pre_get_posts', 'SearchFilter');

I implemented the above code, but the categories that are not being searched get stuck in the search.
Also,
Category IDs that are not searched for in the plug-in "Search Everything" configuration. If you register and search, the category ID you registered will not be searched.

There is probably another way to implement it, so if you know it,
Professor, please.

php wordpress

2022-09-30 19:56

1 Answers

I am writing to let you know that I have solved myself.
Simply implement the following code in functions.php

// Associate FAQs with Categories
function searchJoin($join){
    if(is_search(){
        $join=$join. "INNER JOIN wp_term_relationships ON(wp_term_relationships.object_id=wp_posts.ID);
    }
    return$join;
}
add_filter('posts_join','searchJoin');

// Exclude applicable categories from search criteria
function searchFilter($where,$query){
    if(!is_admin()&$query->is_main_query()&$query->is_search(){
        $excl_list = "Topic (category) IDs to exclude (separated by commas)";
        $where = $where. "AND(wp_term_relationships.term_taxonomy_id NOT IN("."$excl_list."))AND(wp_posts.post_type IN('question'));
    }
    return$where;
}
add_filter('posts_search','searchFilter',10,2);

I hope it will be helpful.


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.