Commit 0e937585 authored by Noemie Kerroux's avatar Noemie Kerroux

Update search.php for tags and post_metas

parent b432cb6d
......@@ -22,11 +22,13 @@ class WPGlobalSearch {
'posts_per_page' => (!empty($args['posts_per_page'])) ? $args['posts_per_page'] : 12,
'search_if_empty' => (!empty($args['search_if_empty'])) ? $args['search_if_empty'] : true,
'taxonomies' => (!empty($args['taxonomies'])) ? $args['taxonomies'] : [],
'post_metas' => (!empty($args['post_metas'])) ? $args['post_metas'] : [],
'search_on' => [
'post_title' => ( (!empty($args['search_on']) && isset($args['search_on']['post_title']) ) ? $args['search_on']['post_title'] : true ),
'post_content' => ( (!empty($args['search_on']) && isset($args['search_on']['post_content'])) ? $args['search_on']['post_content'] : true ),
'post_metas' => ( (!empty($args['search_on']) && isset($args['search_on']['post_metas'])) ? $args['search_on']['post_metas'] : true ),
]
'post_tags' => ( (!empty($args['search_on']) && isset($args['search_on']['post_tags'])) ? $args['search_on']['post_tags'] : true ),
],
];
$articles = [];
......@@ -41,13 +43,16 @@ class WPGlobalSearch {
$post_content_likes = "";
$t_name_likes = "";
$post_metas_likes = "";
$post_metas_keys = self::get_formatted_sql_element($default_args['post_metas']);
$array_keys = array_keys($query_array);
$last_key = end($array_keys);
foreach ($query_array as $key => $q) {
if($default_args['search_on']['post_title']){ $post_title_likes .= "p.post_title LIKE '%" . $q . "%' "; }
if($default_args['search_on']['post_content']){ $post_content_likes .= "p.post_content LIKE '%" . $q . "%' "; }
$t_name_likes .= "t.name LIKE '%" . $q . "%' ";
if($default_args['search_on']['post_metas']){ $post_metas_likes .= "pm.meta_value LIKE '%" . $q . "%' "; }
if($default_args['search_on']['post_metas']){
$post_metas_likes .= "pm.meta_value LIKE '%" . $q . "%' ";
}
if($key != $last_key){
if($default_args['search_on']['post_title']){ $post_title_likes .= "OR "; }
if($default_args['search_on']['post_content']){ $post_content_likes .= "OR "; }
......@@ -55,13 +60,16 @@ class WPGlobalSearch {
if($default_args['search_on']['post_metas']){ $post_metas_likes .= "OR "; }
}
}
if($default_args['search_on']['post_metas'] && !empty($default_args['post_metas'])){
$post_metas_likes .= " AND pm.meta_key IN " . $post_metas_keys . " ";
}
if( ( !empty($query) ) || ( $default_args['search_if_empty'] == true ) ){
$offset = ($page - 1) * $default_args['posts_per_page'];
$db_query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT(p.ID), p.* FROM {$wpdb->base_prefix}posts p
LEFT JOIN {$wpdb->base_prefix}term_relationships tr ON tr.object_id LIKE p.ID ";
if(!empty($default_args['taxonomies'])){
if(!empty($default_args['taxonomies']) || !$default_args['search_on']['post_tags']){
$db_query .= "LEFT JOIN {$wpdb->base_prefix}term_taxonomy tt ON tt.term_id LIKE tr.term_taxonomy_id ";
}
$db_query .= "LEFT JOIN {$wpdb->base_prefix}terms t ON tr.term_taxonomy_id LIKE t.term_id
......@@ -112,6 +120,10 @@ class WPGlobalSearch {
)
)";
}
if(!$default_args['search_on']['post_tags']){
$db_query .= " AND tt.taxonomy NOT LIKE 'post_tag' ";
}
$db_query .= "ORDER BY p.post_date DESC
LIMIT " . $default_args['posts_per_page'] . " OFFSET " . $offset . ";";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment