Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function list_searcheable_acf(){
- $list_searcheable_acf = array('title', 'sub_title', 'excerpt_short', 'excerpt_long', 'codigo', 'cor', 'desenho', 'variacao', 'comissao_vendedor', 'largura_vies', 'valor_unitario', 'quantidade', 'valor_total');
- return $list_searcheable_acf;
- }
- function advanced_custom_search($where, &$wp_query ){
- global $wpdb;
- if(empty($where))
- return $where;
- // get search expression
- $terms = $wp_query->query_vars[ 's' ];
- // explode search expression to get search terms
- $exploded = explode( ' ', $terms );
- if($exploded === FALSE || count($exploded) == 0)
- $exploded = array( 0 => $terms );
- // reset search in order to rebuilt it as we whish
- $where = '';
- // get searcheable_acf, a list of advanced custom fields you want to search content in
- $list_searcheable_acf = list_searcheable_acf();
- foreach($exploded as $tag):
- $where .= "
- AND (
- (wp_posts.post_title LIKE '%$tag%')
- OR (wp_posts.post_content LIKE '%$tag%')
- OR EXISTS (
- SELECT * FROM wp_postmeta
- WHERE post_id = wp_posts.ID
- AND (";
- foreach($list_searcheable_acf as $searcheable_acf):
- if($searcheable_acf == $list_searcheable_acf[0]):
- $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
- else:
- $where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
- endif;
- endforeach;
- $where .= ")
- )
- OR EXISTS (
- SELECT * FROM wp_comments
- WHERE comment_post_ID = wp_posts.ID
- AND comment_content LIKE '%$tag%'
- )
- OR EXISTS (
- SELECT * FROM wp_terms
- INNER JOIN wp_term_taxonomy
- ON wp_term_taxonomy.term_id = wp_terms.term_id
- INNER JOIN wp_term_relationships
- ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
- WHERE (
- taxonomy = 'post_tag'
- OR taxonomy = 'category'
- OR taxonomy = 'myCustomTax'
- )
- AND object_id = wp_posts.ID
- AND wp_terms.name LIKE '%$tag%'
- )
- )";
- endforeach;
- return $where;
- }add_filter('posts_search', 'advanced_custom_search', 500, 2);
- function cf_search_join($join){
- global $wpdb;
- if(is_search()):
- $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
- endif;
- return $join;
- }add_filter('posts_join', 'cf_search_join' );
- function cf_search_where($where){
- global $pagenow, $wpdb;
- if(is_search()):
- $where = preg_replace("/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where);
- endif;
- return $where;
- }add_filter('posts_where', 'cf_search_where');
- function cf_search_distinct($where){
- global $wpdb;
- if(is_search()):
- return 'DISTINCT';
- endif;
- return $where;
- }add_filter('posts_distinct', 'cf_search_distinct');
Add Comment
Please, Sign In to add comment