Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boldairdev/ff0d04ca797a2dacdcb5d72e6c0587ba to your computer and use it in GitHub Desktop.
Save Boldairdev/ff0d04ca797a2dacdcb5d72e6c0587ba to your computer and use it in GitHub Desktop.
PHP - Wordpress - Search - wordpress custom search function that encompasses ACF/advanced custom fields and taxonomies and split expression before request
<?php
**
* [list_searcheable_acf list all the custom fields we want to include in our search query]
* each array member is the name (slug) of the key we want to search
* @return [array] [list of custom fields]
*/
function list_searcheable_acf(){
$list_searcheable_acf = array("productean", "productidentart", "productnumart", "productauthors", "_sku");
return $list_searcheable_acf;
}
/**
* [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression before request]
* @param [query-part/string] $where [the initial "where" part of the search query]
* @param [object] $wp_query []
* @return [query-part/string] $where [the "where" part of the search query as we customized]
* see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/
* credits to Vincent Zurczak for the base query structure/spliting tags section
*/
function advanced_custom_search( $where, $wp_query ) {
global $wpdb;
if ( empty( $where ))
return $where;
// 1- get search expression
$terms_raw = $wp_query->query_vars[ 's' ];
// 2- check search term for XSS attacks
$terms_xss_cleared = strip_tags($terms_raw);
// 3- do another check for SQL injection, use WP esc_sql
$terms = esc_sql($terms_xss_cleared);
// 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 (
(". $wpdb->prefix ."posts.post_title LIKE '%$tag%')
OR (". $wpdb->prefix ."posts.post_content LIKE '%$tag%')
OR EXISTS (
SELECT * FROM ". $wpdb->prefix ."postmeta
WHERE post_id = ". $wpdb->prefix ."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 ". $wpdb->prefix ."comments
WHERE comment_post_ID = ". $wpdb->prefix ."posts.ID
AND comment_content LIKE '%$tag%'
)
OR EXISTS (
SELECT * FROM ". $wpdb->prefix ."terms
INNER JOIN ". $wpdb->prefix ."term_taxonomy
ON ". $wpdb->prefix ."term_taxonomy.term_id = ". $wpdb->prefix ."terms.term_id
INNER JOIN ". $wpdb->prefix ."term_relationships
ON ". $wpdb->prefix ."term_relationships.term_taxonomy_id = ". $wpdb->prefix ."term_taxonomy.term_taxonomy_id
WHERE (
taxonomy = 'post_tag'
OR taxonomy = 'category'
OR taxonomy = 'product_cat'
OR taxonomy = 'product_tag'
)
AND object_id = ". $wpdb->prefix ."posts.ID
AND ". $wpdb->prefix ."terms.name LIKE '%$tag%'
)
)";
endforeach;
return $where;
}
add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment