Guest User

Untitled

a guest
Dec 16th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. function list_searcheable_acf(){
  2.  
  3. $list_searcheable_acf = array('title', 'sub_title', 'excerpt_short', 'excerpt_long', 'codigo', 'cor', 'desenho', 'variacao', 'comissao_vendedor', 'largura_vies', 'valor_unitario', 'quantidade', 'valor_total');
  4.  
  5. return $list_searcheable_acf;
  6.  
  7. }
  8.  
  9. function advanced_custom_search($where, &$wp_query ){
  10.  
  11. global $wpdb;
  12.  
  13. if(empty($where))
  14.  
  15. return $where;
  16.  
  17. // get search expression
  18. $terms = $wp_query->query_vars[ 's' ];
  19.  
  20. // explode search expression to get search terms
  21. $exploded = explode( ' ', $terms );
  22.  
  23. if($exploded === FALSE || count($exploded) == 0)
  24.  
  25. $exploded = array( 0 => $terms );
  26.  
  27. // reset search in order to rebuilt it as we whish
  28. $where = '';
  29.  
  30. // get searcheable_acf, a list of advanced custom fields you want to search content in
  31. $list_searcheable_acf = list_searcheable_acf();
  32.  
  33. foreach($exploded as $tag):
  34.  
  35. $where .= "
  36. AND (
  37. (wp_posts.post_title LIKE '%$tag%')
  38. OR (wp_posts.post_content LIKE '%$tag%')
  39. OR EXISTS (
  40. SELECT * FROM wp_postmeta
  41. WHERE post_id = wp_posts.ID
  42. AND (";
  43.  
  44. foreach($list_searcheable_acf as $searcheable_acf):
  45.  
  46. if($searcheable_acf == $list_searcheable_acf[0]):
  47.  
  48. $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
  49.  
  50. else:
  51.  
  52. $where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
  53.  
  54. endif;
  55.  
  56. endforeach;
  57.  
  58. $where .= ")
  59. )
  60. OR EXISTS (
  61. SELECT * FROM wp_comments
  62. WHERE comment_post_ID = wp_posts.ID
  63. AND comment_content LIKE '%$tag%'
  64. )
  65. OR EXISTS (
  66. SELECT * FROM wp_terms
  67. INNER JOIN wp_term_taxonomy
  68. ON wp_term_taxonomy.term_id = wp_terms.term_id
  69. INNER JOIN wp_term_relationships
  70. ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
  71. WHERE (
  72. taxonomy = 'post_tag'
  73. OR taxonomy = 'category'
  74. OR taxonomy = 'myCustomTax'
  75. )
  76. AND object_id = wp_posts.ID
  77. AND wp_terms.name LIKE '%$tag%'
  78. )
  79. )";
  80.  
  81. endforeach;
  82.  
  83. return $where;
  84.  
  85. }add_filter('posts_search', 'advanced_custom_search', 500, 2);
  86.  
  87.  
  88. function cf_search_join($join){
  89.  
  90. global $wpdb;
  91.  
  92. if(is_search()):
  93.  
  94. $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
  95.  
  96. endif;
  97.  
  98. return $join;
  99.  
  100. }add_filter('posts_join', 'cf_search_join' );
  101.  
  102.  
  103. function cf_search_where($where){
  104.  
  105. global $pagenow, $wpdb;
  106.  
  107. if(is_search()):
  108.  
  109. $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);
  110.  
  111. endif;
  112.  
  113. return $where;
  114.  
  115. }add_filter('posts_where', 'cf_search_where');
  116.  
  117.  
  118. function cf_search_distinct($where){
  119.  
  120. global $wpdb;
  121.  
  122. if(is_search()):
  123.  
  124. return 'DISTINCT';
  125.  
  126. endif;
  127.  
  128. return $where;
  129.  
  130. }add_filter('posts_distinct', 'cf_search_distinct');
Add Comment
Please, Sign In to add comment