feat: update build where in general search
This commit is contained in:
parent
314e1f3114
commit
310ca1ed02
|
@ -39,18 +39,14 @@ class Search_Engine {
|
|||
|
||||
function search_hooks() {
|
||||
|
||||
add_filter( 'posts_join', array( &$this, 'terms_join' ) );
|
||||
|
||||
add_filter( 'posts_join', array( &$this, 'search_metadata_join' ) );
|
||||
|
||||
// add_filter( 'posts_join', array( &$this, 'terms_join' ) );
|
||||
// add_filter( 'posts_join', array( &$this, 'search_metadata_join' ) );
|
||||
add_filter( 'posts_join', array( &$this, 'relationships_join' ) );
|
||||
|
||||
//add_filter( 'posts_where', array( &$this, 'search_attachments' ) );
|
||||
|
||||
add_filter( 'posts_search', array( &$this, 'search_where' ), 10, 2 );
|
||||
|
||||
add_filter( 'posts_request', array( &$this, 'distinct' ) );
|
||||
|
||||
//add_filter( 'posts_request', array( &$this, 'log_query' ), 10, 2 );
|
||||
}
|
||||
|
||||
|
@ -120,6 +116,72 @@ class Search_Engine {
|
|||
}
|
||||
}
|
||||
|
||||
private function build_search_terms_query ($type = 'default') {
|
||||
global $wpdb;
|
||||
$searchQuery = '';
|
||||
$seperator = '';
|
||||
$not_exact = empty($this->query_instance->query_vars['exact']);
|
||||
$terms = $this->get_search_terms();
|
||||
foreach ( $terms as $term ) {
|
||||
$esc_term = $wpdb->prepare("%s", $not_exact ? "%".$term."%" : $term);
|
||||
switch ($type) {
|
||||
case 'tax_terms':
|
||||
$searchQuery .= "{$seperator}(tter.name LIKE {$esc_term})";
|
||||
break;
|
||||
case 'meta_terms':
|
||||
if (defined('TAINACAN_SEARCH_ENGINE_USING_FULL_TEXT') && TAINACAN_SEARCH_ENGINE_USING_FULL_TEXT === true) {
|
||||
$esc_term = $wpdb->prepare("%s", $not_exact ? "*".$term."*" : $term);
|
||||
$searchQuery .= "{$seperator}(MATCH (m.meta_value) AGAINST ({$esc_term} IN BOOLEAN MODE))";
|
||||
} else {
|
||||
$searchQuery .= "{$seperator}(m.meta_value LIKE {$esc_term})";
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if ( !empty( $this->relationships ) ) {
|
||||
$searchQuery .= "{$seperator}($wpdb->posts.post_title LIKE {$esc_term} OR $wpdb->posts.post_content LIKE {$esc_term} OR p2->posts.post_title LIKE {$esc_term} OR p2.post_content LIKE {$esc_term})";
|
||||
} else {
|
||||
$searchQuery .= "{$seperator}($wpdb->posts.post_title LIKE {$esc_term} OR $wpdb->posts.post_content LIKE {$esc_term})";
|
||||
}
|
||||
break;
|
||||
}
|
||||
$seperator = ' OR ';
|
||||
}
|
||||
return empty($searchQuery) ? false : "($searchQuery)";
|
||||
}
|
||||
|
||||
function get_where_to_term_taxonomies() {
|
||||
global $wpdb;
|
||||
$join = '';
|
||||
$searchTaxQuery = $this->build_search_terms_query('tax_terms');
|
||||
if ( $searchTaxQuery != false && $this->is_tainacan_search && !empty( $this->taxonomies ) ) {
|
||||
$tax_where = ' ttax.taxonomy IN ( \'' . implode( '\',\'', $this->taxonomies ) . '\' ) ';
|
||||
$join = "EXISTS (
|
||||
SELECT trel.object_id
|
||||
FROM
|
||||
$wpdb->term_relationships AS trel
|
||||
INNER JOIN $wpdb->term_taxonomy AS ttax ON trel.term_taxonomy_id = ttax.term_taxonomy_id
|
||||
INNER JOIN $wpdb->terms AS tter ON ttax.term_id = tter.term_id
|
||||
WHERE
|
||||
$wpdb->posts.ID = trel.object_id AND $tax_where AND ( $searchTaxQuery )
|
||||
)";
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
|
||||
function get_where_to_metadatas() {
|
||||
global $wpdb;
|
||||
$join = "";
|
||||
$searchMetaQuery = $this->build_search_terms_query('meta_terms');
|
||||
if ( $searchMetaQuery != false && $this->is_tainacan_search ) {
|
||||
$join .= "EXISTS (
|
||||
SELECT m.post_id
|
||||
FROM $wpdb->postmeta m
|
||||
WHERE ( $wpdb->posts.ID = m.post_id AND $searchMetaQuery )
|
||||
)";
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
|
||||
// add where clause to the search query
|
||||
function search_where( $where, $wp_query ) {
|
||||
|
||||
|
@ -134,7 +196,11 @@ class Search_Engine {
|
|||
return $where;
|
||||
|
||||
$searchQuery = $this->build_search_terms_query();
|
||||
$searchQuery = "($searchQuery) OR (tax_terms.contains = TRUE) OR (metas.contains = TRUE) ";
|
||||
$searchTaxQuery = $this->get_where_to_term_taxonomies();
|
||||
$searchMetaQuery = $this->get_where_to_metadatas();
|
||||
$searchQuery = "($searchQuery) ";
|
||||
if(!empty($searchTaxQuery)) $searchQuery .= " OR ($searchTaxQuery) ";
|
||||
if(!empty($searchMetaQuery)) $searchQuery .= " OR ($searchMetaQuery) ";
|
||||
|
||||
if ( $searchQuery != '' && $searchQuery != '()' ) {
|
||||
// lets use _OUR_ query instead of WP's, as we have posts already included in our query as well(assuming it's not empty which we check for)
|
||||
|
@ -154,49 +220,6 @@ class Search_Engine {
|
|||
return $query;
|
||||
}
|
||||
|
||||
|
||||
//search attachments
|
||||
function search_attachments( $where ) {
|
||||
global $wpdb;
|
||||
if ( !empty( $this->query_instance->query_vars['s'] ) ) {
|
||||
$where = str_replace( '"', '\'', $where );
|
||||
if ( !$this->wp_ver28 ) {
|
||||
$where = str_replace( " AND (post_status = 'publish'", " AND (post_status = 'publish' OR post_type = 'attachment'", $where );
|
||||
$where = str_replace( "AND post_type != 'attachment'", "", $where );
|
||||
}
|
||||
else {
|
||||
$where = str_replace( " AND ($wpdb->posts.post_status = 'publish'", " AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_type = 'attachment'", $where );
|
||||
$where = str_replace( "AND $wpdb->posts.post_type != 'attachment'", "", $where );
|
||||
}
|
||||
}
|
||||
return $where;
|
||||
}
|
||||
|
||||
//join for searching metadata
|
||||
function search_metadata_join( $join ) {
|
||||
|
||||
if ($this->is_inner_query) {
|
||||
return $join;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
if ( $this->is_tainacan_search ) {
|
||||
$searchMetaQuery = $this->build_search_terms_query('meta_terms');
|
||||
$join .= <<<EOF
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT
|
||||
m.post_id, true as contains
|
||||
FROM
|
||||
$wpdb->postmeta m
|
||||
WHERE
|
||||
( $searchMetaQuery )
|
||||
) AS metas ON $wpdb->posts.ID = metas.post_id
|
||||
EOF;
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
|
||||
// join for relationship metadata
|
||||
function relationships_join( $join ) {
|
||||
|
||||
|
@ -214,61 +237,74 @@ EOF;
|
|||
return $join;
|
||||
}
|
||||
|
||||
//join for searching taxonomies terms
|
||||
function terms_join( $join ) {
|
||||
|
||||
if ($this->is_inner_query) {
|
||||
return $join;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$searchTaxQuery = $this->build_search_terms_query('tax_terms');
|
||||
if ( $this->is_tainacan_search && !empty( $this->taxonomies ) ) {
|
||||
$tax_where = ' ttax.taxonomy IN ( \'' . implode( '\',\'', $this->taxonomies ) . '\' ) ';
|
||||
$join .= <<<EOF
|
||||
LEFT JOIN (
|
||||
SELECT DISTINCT
|
||||
trel.object_id as post_id,
|
||||
true as contains
|
||||
FROM
|
||||
$wpdb->term_relationships AS trel
|
||||
INNER JOIN $wpdb->term_taxonomy AS ttax ON trel.term_taxonomy_id = ttax.term_taxonomy_id
|
||||
INNER JOIN $wpdb->terms AS tter ON ttax.term_id = tter.term_id
|
||||
WHERE
|
||||
$tax_where AND ( $searchTaxQuery )
|
||||
) tax_terms ON $wpdb->posts.ID = tax_terms.post_id
|
||||
EOF;
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
// //search attachments
|
||||
// function search_attachments( $where ) {
|
||||
// global $wpdb;
|
||||
// if ( !empty( $this->query_instance->query_vars['s'] ) ) {
|
||||
// $where = str_replace( '"', '\'', $where );
|
||||
// if ( !$this->wp_ver28 ) {
|
||||
// $where = str_replace( " AND (post_status = 'publish'", " AND (post_status = 'publish' OR post_type = 'attachment'", $where );
|
||||
// $where = str_replace( "AND post_type != 'attachment'", "", $where );
|
||||
// }
|
||||
// else {
|
||||
// $where = str_replace( " AND ($wpdb->posts.post_status = 'publish'", " AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_type = 'attachment'", $where );
|
||||
// $where = str_replace( "AND $wpdb->posts.post_type != 'attachment'", "", $where );
|
||||
// }
|
||||
// }
|
||||
// return $where;
|
||||
// }
|
||||
|
||||
private function build_search_terms_query ($type = 'default') {
|
||||
global $wpdb;
|
||||
$searchQuery = '(';
|
||||
$seperator = '';
|
||||
$not_exact = empty($this->query_instance->query_vars['exact']);
|
||||
$terms = $this->get_search_terms();
|
||||
foreach ( $terms as $term ) {
|
||||
$esc_term = $wpdb->prepare("%s", $not_exact ? "%".$term."%" : $term);
|
||||
switch ($type) {
|
||||
case 'tax_terms':
|
||||
$searchQuery .= "{$seperator}(tter.name LIKE {$esc_term})";
|
||||
break;
|
||||
case 'meta_terms':
|
||||
$searchQuery .= "{$seperator}(m.meta_value LIKE {$esc_term})";
|
||||
break;
|
||||
default:
|
||||
if ( !empty( $this->relationships ) ) {
|
||||
$searchQuery .= "{$seperator}($wpdb->posts.post_title LIKE {$esc_term} OR $wpdb->posts.post_content LIKE {$esc_term} OR p2->posts.post_title LIKE {$esc_term} OR p2.post_content LIKE {$esc_term})";
|
||||
} else {
|
||||
$searchQuery .= "{$seperator}($wpdb->posts.post_title LIKE {$esc_term} OR $wpdb->posts.post_content LIKE {$esc_term})";
|
||||
}
|
||||
break;
|
||||
}
|
||||
$seperator = ' AND ';
|
||||
}
|
||||
$searchQuery .= ')';
|
||||
return $searchQuery;
|
||||
}
|
||||
}
|
||||
// //join for searching metadata
|
||||
// function search_metadata_join( $join ) {
|
||||
|
||||
// if ($this->is_inner_query) {
|
||||
// return $join;
|
||||
// }
|
||||
|
||||
// global $wpdb;
|
||||
// if ( $this->is_tainacan_search ) {
|
||||
// $searchMetaQuery = $this->build_search_terms_query('meta_terms');
|
||||
// $join .= <<<EOF
|
||||
// LEFT JOIN
|
||||
// (
|
||||
// SELECT
|
||||
// m.post_id, true as contains
|
||||
// FROM
|
||||
// $wpdb->postmeta m
|
||||
// WHERE
|
||||
// ( $searchMetaQuery )
|
||||
// ) AS metas ON $wpdb->posts.ID = metas.post_id
|
||||
// EOF;
|
||||
// }
|
||||
// return $join;
|
||||
// }
|
||||
|
||||
// //join for searching taxonomies terms
|
||||
// function terms_join( $join ) {
|
||||
|
||||
// if ($this->is_inner_query) {
|
||||
// return $join;
|
||||
// }
|
||||
|
||||
// global $wpdb;
|
||||
// $searchTaxQuery = $this->build_search_terms_query('tax_terms');
|
||||
// if ( $this->is_tainacan_search && !empty( $this->taxonomies ) ) {
|
||||
// $tax_where = ' ttax.taxonomy IN ( \'' . implode( '\',\'', $this->taxonomies ) . '\' ) ';
|
||||
// $join .= <<<EOF
|
||||
// LEFT JOIN (
|
||||
// SELECT DISTINCT
|
||||
// trel.object_id as post_id,
|
||||
// true as contains
|
||||
// FROM
|
||||
// $wpdb->term_relationships AS trel
|
||||
// INNER JOIN $wpdb->term_taxonomy AS ttax ON trel.term_taxonomy_id = ttax.term_taxonomy_id
|
||||
// INNER JOIN $wpdb->terms AS tter ON ttax.term_id = tter.term_id
|
||||
// WHERE
|
||||
// $tax_where AND ( $searchTaxQuery )
|
||||
// ) tax_terms ON $wpdb->posts.ID = tax_terms.post_id
|
||||
// EOF;
|
||||
// }
|
||||
// return $join;
|
||||
// }
|
||||
|
||||
}
|
Loading…
Reference in New Issue