admin管理员组文章数量:1312945
In my theme's search.php
I have a section set up to show terms that fit the search. I want the term to show up IF the $keyword
appears in the term's title or its description. I have it set up to do just that but if feels clunky to me to have to do two separate queries then prune the results to make sure each term is only displayed once.
$search_matters_args = array(
'taxonomy' => array('book', 'magazine'), // taxonomies to search
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'number' => 8,
'name__like' => $keyword,
);
$name_search = new WP_Term_Query($search_matters_args);
/*--- Query again on description ---*/
$search_matters_args['name__like'] = ''; // Override for next query
$search_matters_args['description__like'] = $keyword; // Override for next query
$desc_search = new WP_Term_Query($search_matters_args);
$books_and_magazines = array_merge($name_search->terms, $desc_search->terms);
$filtered_topics = array();
if (!empty($books_and_magazines) && !is_wp_error($books_and_magazines)) {
$unique_ids = array();
for ($i=0; $i < count($books_and_magazines); $i++) {
$termID = $books_and_magazines[$i]->term_id;
if (in_array($termID, $unique_ids)) {
continue;
} else {
$unique_ids[] = $termID;
$filtered_topics[] = $books_and_magazines[$i];
}
} // End For loop
$books_and_magazines = $filtered_topics;
} // End if $books_and_magazines is not empty or WP Error
Is there a more efficient way to query based on both? Thank you!
In my theme's search.php
I have a section set up to show terms that fit the search. I want the term to show up IF the $keyword
appears in the term's title or its description. I have it set up to do just that but if feels clunky to me to have to do two separate queries then prune the results to make sure each term is only displayed once.
$search_matters_args = array(
'taxonomy' => array('book', 'magazine'), // taxonomies to search
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'number' => 8,
'name__like' => $keyword,
);
$name_search = new WP_Term_Query($search_matters_args);
/*--- Query again on description ---*/
$search_matters_args['name__like'] = ''; // Override for next query
$search_matters_args['description__like'] = $keyword; // Override for next query
$desc_search = new WP_Term_Query($search_matters_args);
$books_and_magazines = array_merge($name_search->terms, $desc_search->terms);
$filtered_topics = array();
if (!empty($books_and_magazines) && !is_wp_error($books_and_magazines)) {
$unique_ids = array();
for ($i=0; $i < count($books_and_magazines); $i++) {
$termID = $books_and_magazines[$i]->term_id;
if (in_array($termID, $unique_ids)) {
continue;
} else {
$unique_ids[] = $termID;
$filtered_topics[] = $books_and_magazines[$i];
}
} // End For loop
$books_and_magazines = $filtered_topics;
} // End if $books_and_magazines is not empty or WP Error
Is there a more efficient way to query based on both? Thank you!
Share Improve this question asked May 22, 2018 at 17:51 StephanieQStephanieQ 4875 silver badges12 bronze badges2 Answers
Reset to default 1I don't see any options for WordPress to search terms by both of name & description. So i combined 2 queries like @StephanieQ but maybe more simple way, checkout my ajax response below:
public function ajax_project_terms_search() {
$results = array();
$search_query = sanitize_text_field($_GET['q']);
$withname = intval($_GET['withname']);
$search_keys = array('name__like', 'description__like');
$exclude = array();
foreach ($search_keys as $search_key) {
$terms = get_terms( array(
'taxonomy' => 'project',
'hide_empty' => false,
'number' => 8,
'exclude' => $exclude,
$search_key => $search_query,
));
// create results
foreach ($terms as $term) {
$exclude[] = $term->term_id;
$results[] = array(
'id' => isset($_GET['return_id']) ? $term->term_id : $term->slug,
'text' => $withname ? $term->name . ' - ' . $term->description : $term->name,
);
}
}
wp_send_json($results);
wp_die();
}
I have also search for this but did not find solution, @harisrozak solution is great, but I need to also use order by.
so I had develop custom query for that as below.
function searchterm(){
global $wpdb;
$search_query = sanitize_text_field($_GET['q']);
$query = "SELECT DISTINCT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_termmeta ON ( t.term_id = wp_termmeta.term_id ) INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('category') AND (t.name LIKE 'search_query%' OR tt.description LIKE 'search_query%') AND (
wp_termmeta.meta_key = 'mata_key'
) ORDER BY wp_termmeta.meta_value+0 DESC limit 10"
$the_query = $wpdb->get_results($query);
wp_send_json($the_query);
wp_die();
}
I hope someone get help by this.
本文标签: Search TermsQuerying on either descriptionlike OR namelike in the same Term Query
版权声明:本文标题:Search Terms - Querying on either description__like OR name__like in the same Term Query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741870016a2402136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论