Forked from fiskhandlarn/custom-search-acf-wordpress.php
Last active
April 19, 2021 15:11
Revisions
-
felthy revised this gist
Apr 30, 2017 . 1 changed file with 76 additions and 82 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,24 +6,10 @@ Included are steps to help make this script easier for other to follow All you have to do is add custom ACF post types into Step 1 and custom taxonomies into Step 10 [list_searcheable_acf list all the custom fields we want to include in our search query] @return [array] [list of custom fields] */ /* * [advanced_custom_search search that encompasses ACF/advanced custom fields and taxonomies and split expression before request] * @param [query-part/string] $search [the initial "where" part of the search query] @@ -36,90 +22,98 @@ function list_searcheable_acf(){ function advanced_custom_search( $search, &$wp_query ) { global $wpdb; if (empty($search)) { return $search; } // AND (((wp_posts.post_title LIKE '%venenatis%') OR (wp_posts.post_excerpt LIKE '%venenatis%') OR (wp_posts.post_content LIKE '%venenatis%'))) AND (wp_posts.post_password = '') // 1- get search expression $terms = $wp_query->query_vars['s']; // 2- explode search expression to get search terms $exploded = preg_split('|\s+|', $terms); if ($exploded === FALSE || count($exploded) == 0) { $exploded = array(0 => $terms); } // 3- setup search variable as a string $search = ''; $params = array(); // 4- a list of advanced custom fields you want to search content in $list_searcheable_acf = array( "your", "acf", "non-repeater", "field-names", "here", "repeater" => array( "repeater-sub-field1", "repeater-sub-field2" ) ); // 5- search through tags, inject each into SQL query foreach ($exploded as $tag) { $s = '%' . $wpdb->esc_like($tag) . '%'; $search .= " AND ( ({$wpdb->posts}.post_title LIKE '%s') OR ({$wpdb->posts}.post_excerpt LIKE '%s') OR ({$wpdb->posts}.post_content LIKE '%s') " . // 8- Adds to $search DB data from custom post types "OR EXISTS ( SELECT * FROM {$wpdb->postmeta} WHERE post_id = {$wpdb->posts}.ID AND ("; $params []= $s; $params []= $s; $params []= $s; // 5b - reads through $list_searcheable_acf array to see which custom post types you want to include in the search string $metaStatements = array(); foreach ($list_searcheable_acf as $key => $searcheable_acf) { if (is_array($searcheable_acf)) { foreach ($searcheable_acf as $repeater_acf) { array_push($metaStatements, "(meta_key LIKE '" . $key . "_%%_" . $repeater_acf . "' AND meta_value LIKE '%s')"); $params []= $s; } } else { array_push($metaStatements, "(meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%s')"); $params []= $s; } } $search .= join($metaStatements, "\n OR "); $search .= ") ) " . // 6- Adds to $search DB data from comments "OR EXISTS ( SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = {$wpdb->posts}.ID AND comment_content LIKE '%s' ) " . // 7 - Adds to $search DB data from taxonomies "OR EXISTS ( SELECT * FROM {$wpdb->terms} INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id " . // 7b- Add custom taxonomies here "WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'here' ) AND object_id = {$wpdb->posts}.ID AND {$wpdb->terms}.name LIKE '%s' )" . ")"; $params []= $s; $params []= $s; } return $wpdb->prepare($search, $params); } // closes function advanced_custom_search // 8- use add_filter to put advanced_custom_search into the posts_search results add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); -
fiskhandlarn revised this gist
Dec 9, 2016 . 1 changed file with 63 additions and 56 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,27 @@ <?php /* ############################## ########### Search ########### ############################## Included are steps to help make this script easier for other to follow All you have to do is add custom ACF post types into Step 1 and custom taxonomies into Step 10 I also updated this work to include XSS and SQL injection projection [list_searcheable_acf list all the custom fields we want to include in our search query] @return [array] [list of custom fields] */ // Define list of ACF fields you want to search through - do NOT include taxonomies here function list_searcheable_acf(){ $list_searcheable_acf = array( "your", "acf", "non-repeater", "field-names", "here", "repeater" => array( "repeater-sub-field1", "repeater-sub-field2" ) ); return $list_searcheable_acf; } /* @@ -33,58 +35,63 @@ function list_searcheable_acf(){ */ function advanced_custom_search( $search, &$wp_query ) { global $wpdb; if ( empty( $search )) { return $search; } // 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); // 4- explode search expression to get search terms $exploded = explode( ' ', $terms ); if( $exploded === FALSE || count( $exploded ) == 0 ) { $exploded = array( 0 => $terms ); } // 5- setup search variable as a string $search = ''; // 6- get searcheable_acf, a list of advanced custom fields you want to search content in $list_searcheable_acf = list_searcheable_acf(); // 7- get custom table prefixes, thanks to Brian Douglas @bmdinteractive on github for this improvement $table_prefix = $wpdb->prefix; // 8- search through tags, inject each into SQL query foreach( $exploded as $tag ) { $search .= " AND ( (".$table_prefix."posts.post_title LIKE '%$tag%') OR (".$table_prefix."posts.post_excerpt LIKE '%$tag%') OR (".$table_prefix."posts.post_content LIKE '%$tag%') ". // 9- Adds to $search DB data from custom post types "OR EXISTS ( SELECT * FROM ".$table_prefix."postmeta WHERE post_id = ".$table_prefix."posts.ID AND ("; // 9b - reads through $list_searcheable_acf array to see which custom post types you want to include in the search string $metaStatements = array(); foreach ($list_searcheable_acf as $key => $searcheable_acf) { if ( is_array( $searcheable_acf ) ) { foreach ( $searcheable_acf as $repeater_acf ) { array_push( $metaStatements, "(meta_key LIKE '" . $key . "_%_" . $repeater_acf . "' AND meta_value LIKE '%$tag%')" ); } } else { array_push( $metaStatements, "(meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%$tag%')" ); } } $search .= join( $metaStatements, "\n OR " ); $search .= ") )". // 10- Adds to $search DB data from comments "OR EXISTS ( @@ -93,14 +100,14 @@ function advanced_custom_search( $search, &$wp_query ) { AND comment_content LIKE '%$tag%' )". // 11 - Adds to $search DB data from taxonomies "OR EXISTS ( SELECT * FROM ".$table_prefix."terms INNER JOIN ".$table_prefix."term_taxonomy ON ".$table_prefix."term_taxonomy.term_id = ".$table_prefix."terms.term_id INNER JOIN ".$table_prefix."term_relationships ON ".$table_prefix."term_relationships.term_taxonomy_id = ".$table_prefix."term_taxonomy.term_taxonomy_id". // 11b- Add custom taxonomies here "WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' @@ -109,9 +116,9 @@ function advanced_custom_search( $search, &$wp_query ) { AND object_id = ".$table_prefix."posts.ID AND ".$table_prefix."terms.name LIKE '%$tag%' )". ")"; // closes $search } // closes foreach return $search; } // closes function advanced_custom_search // 12- use add_filter to put advanced_custom_search into the posts_search results -
fiskhandlarn revised this gist
Dec 9, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -111,6 +111,7 @@ function advanced_custom_search( $search, &$wp_query ) { )". ")"; // closes $search } // closes foreach return $search; } // closes function advanced_custom_search // 12- use add_filter to put advanced_custom_search into the posts_search results -
fiskhandlarn revised this gist
Dec 9, 2016 . 1 changed file with 15 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php /* ############################## ########### Search ########### @@ -46,7 +47,7 @@ function advanced_custom_search( $search, &$wp_query ) { // 3- do another check for SQL injection, use WP esc_sql $terms = esc_sql($terms_xss_cleared); // 4- explode search expression to get search terms $exploded = explode( ' ', $terms ); if( $exploded === FALSE || count( $exploded ) == 0 ) { @@ -67,54 +68,50 @@ function advanced_custom_search( $search, &$wp_query ) { $search .= " AND ( (".$table_prefix."posts.post_title LIKE '%$tag%') OR (".$table_prefix."posts.post_excerpt LIKE '%$tag%') OR (".$table_prefix."posts.post_content LIKE '%$tag%') ". // 9- Adds to $search DB data from custom post types "OR EXISTS ( SELECT * FROM ".$table_prefix."postmeta WHERE post_id = ".$table_prefix."posts.ID AND ("; // 9b - reads through $list_searcheable_acf array to see which custom post types you want to include in the search string foreach ($list_searcheable_acf as $searcheable_acf) { if ($searcheable_acf == $list_searcheable_acf[0]) { $search .= " (meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%$tag%') "; } else { $search .= " OR (meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%$tag%') "; } } $search .= ") )". // 10- Adds to $search DB data from comments "OR EXISTS ( SELECT * FROM ".$table_prefix."comments WHERE comment_post_ID = ".$table_prefix."posts.ID AND comment_content LIKE '%$tag%' )". // 11 - Adds to $search DB data from taxonomies "OR EXISTS ( SELECT * FROM ".$table_prefix."terms INNER JOIN ".$table_prefix."term_taxonomy ON ".$table_prefix."term_taxonomy.term_id = ".$table_prefix."terms.term_id INNER JOIN ".$table_prefix."term_relationships ON ".$table_prefix."term_relationships.term_taxonomy_id = ".$table_prefix."term_taxonomy.term_taxonomy_id". // 11b- Add custom taxonomies here "WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'here' ) AND object_id = ".$table_prefix."posts.ID AND ".$table_prefix."terms.name LIKE '%$tag%' )". ")"; // closes $search } // closes foreach } // closes function advanced_custom_search // 12- use add_filter to put advanced_custom_search into the posts_search results add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); -
jserrao revised this gist
Jul 27, 2016 . 1 changed file with 95 additions and 90 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,115 +1,120 @@ /* ############################## ########### Search ########### ############################## Included are steps to help make this script easier for other to follow All you have to do is add custom ACF post types into Step 1 and custom taxonomies into Step 10 I also updated this work to include XSS and SQL injection projection [list_searcheable_acf list all the custom fields we want to include in our search query] @return [array] [list of custom fields] */ // Define list of ACF fields you want to search through - do NOT include taxonomies here function list_searcheable_acf(){ $list_searcheable_acf = array( "your", "custom", "post-types", "here" ); 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] $search [the initial "where" part of the search query] * @param [object] $wp_query [] * @return [query-part/string] $search [the "where" part of the search query as we customized] * modified from gist: https://gist.github.com/FutureMedia/9581381/73afa809f38527d57f4213581eeae6a8e5a1340a * 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 and Sjouw for comment cleanup */ function advanced_custom_search( $search, &$wp_query ) { global $wpdb; if ( empty( $search )) { return $search; } // 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); // 4- explode search expression to get search terms $exploded = explode( ' ', $terms ); if( $exploded === FALSE || count( $exploded ) == 0 ) { $exploded = array( 0 => $terms ); } // 5- setup search variable as a string $search = ''; // 6- get searcheable_acf, a list of advanced custom fields you want to search content in $list_searcheable_acf = list_searcheable_acf(); // 7- get custom table prefixes, thanks to Brian Douglas @bmdinteractive on github for this improvement $table_prefix = $wpdb->prefix; // 8- search through tags, inject each into SQL query foreach( $exploded as $tag ) { $search .= " AND ( (".$table_prefix."posts.post_title LIKE '%$tag%') OR (".$table_prefix."posts.post_content LIKE '%$tag%') // 9- Adds to $search DB data from custom post types OR EXISTS ( SELECT * FROM ".$table_prefix."postmeta WHERE post_id = ".$table_prefix."posts.ID AND ("; // 9b - reads through $list_searcheable_acf array to see which custom post types you want to include in the search string foreach ($list_searcheable_acf as $searcheable_acf) { if ($searcheable_acf == $list_searcheable_acf[0]) { $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } else { $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } } $search .= ") ) // 10- Adds to $search DB data from comments OR EXISTS ( SELECT * FROM ".$table_prefix."comments WHERE comment_post_ID = ".$table_prefix."posts.ID AND comment_content LIKE '%$tag%' ) // 11 - Adds to $search DB data from taxonomies OR EXISTS ( SELECT * FROM ".$table_prefix."terms INNER JOIN ".$table_prefix."term_taxonomy ON ".$table_prefix."term_taxonomy.term_id = ".$table_prefix."terms.term_id INNER JOIN ".$table_prefix."term_relationships ON ".$table_prefix."term_relationships.term_taxonomy_id = ".$table_prefix."term_taxonomy.term_taxonomy_id // 11b- Add custom taxonomies here WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'here' ) AND object_id = ".$table_prefix."posts.ID AND ".$table_prefix."terms.name LIKE '%$tag%' ) )"; // closes $search } // closes foreach return $search; } // closes function advanced_custom_search // 12- use add_filter to put advanced_custom_search into the posts_search results add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); ?> -
jserrao revised this gist
May 18, 2016 . 1 changed file with 49 additions and 46 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,53 +60,56 @@ function advanced_custom_search( $search, &$wp_query ) { // 6- get searcheable_acf, a list of advanced custom fields you want to search content in $list_searcheable_acf = list_searcheable_acf(); // 7- get custom table prefixes, thanks to Brian Douglas @bmdinteractive on github for this improvement $table_prefix = $wpdb->prefix; // 8- search through tags, inject each into SQL query foreach( $exploded as $tag ) { $search .= " AND ( (".$table_prefix."posts.post_title LIKE '%$tag%') OR (".$table_prefix."posts.post_content LIKE '%$tag%') OR EXISTS ( SELECT * FROM ".$table_prefix."postmeta WHERE post_id = ".$table_prefix."posts.ID AND ("; // 9 - add each custom post-type into SQL query foreach ($list_searcheable_acf as $searcheable_acf) { if ($searcheable_acf == $list_searcheable_acf[0]) { $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } else { $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } } // 10- Add to search string info from comments and custom taxonomies // You would need to customize the taxonomies below to match your site $search .= ") ) OR EXISTS ( SELECT * FROM ".$table_prefix."comments WHERE comment_post_ID = ".$table_prefix."posts.ID AND comment_content LIKE '%$tag%' ) OR EXISTS ( SELECT * FROM ".$table_prefix."terms INNER JOIN ".$table_prefix."term_taxonomy ON ".$table_prefix."term_taxonomy.term_id = ".$table_prefix."terms.term_id INNER JOIN ".$table_prefix."term_relationships ON ".$table_prefix."term_relationships.term_taxonomy_id = ".$table_prefix."term_taxonomy.term_taxonomy_id WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'here' ) AND object_id = ".$table_prefix."posts.ID AND ".$table_prefix."terms.name LIKE '%$tag%' ) )"; } return $search; } // 11- use add_filter to put advanced_custom_search into the posts_search results add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); ?> -
jserrao revised this gist
Mar 13, 2016 . 1 changed file with 23 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,8 +35,9 @@ function list_searcheable_acf(){ function advanced_custom_search( $search, &$wp_query ) { global $wpdb; if ( empty( $search )) { return $search; } // 1- get search expression $terms_raw = $wp_query->query_vars[ 's' ]; @@ -49,17 +50,18 @@ function advanced_custom_search( $search, &$wp_query ) { // 4- explode search expression to get search terms $exploded = explode( ' ', $terms ); if( $exploded === FALSE || count( $exploded ) == 0 ) { $exploded = array( 0 => $terms ); } // 5- setup search variable as a string $search = ''; // 6- get searcheable_acf, a list of advanced custom fields you want to search content in $list_searcheable_acf = list_searcheable_acf(); // 7- search through tags, inject each into SQL query foreach( $exploded as $tag ) { $search .= " AND ( (wp_posts.post_title LIKE '%$tag%') @@ -68,15 +70,16 @@ function advanced_custom_search( $search, &$wp_query ) { SELECT * FROM wp_postmeta WHERE post_id = wp_posts.ID AND ("; // 7b - add each custom post-type into SQL query foreach ($list_searcheable_acf as $searcheable_acf) { if ($searcheable_acf == $list_searcheable_acf[0]) { $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } else { $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; } } // 8- Add to search string info from comments and custom taxonomies // You would need to customize the taxonomies below to match your site $search .= ") ) OR EXISTS ( @@ -91,17 +94,16 @@ function advanced_custom_search( $search, &$wp_query ) { INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id WHERE ( taxonomy = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'here' ) AND object_id = wp_posts.ID AND wp_terms.name LIKE '%$tag%' ) )"; } return $search; } -
jserrao revised this gist
Mar 11, 2016 . No changes.There are no files selected for viewing
-
jserrao revised this gist
Mar 11, 2016 . 1 changed file with 91 additions and 67 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,86 +1,110 @@ /* ############################## ########### Search ########### ############################## */ /** * * [list_searcheable_acf list all the custom fields we want to include in our search query] * @return [array] [list of custom fields] * Included are steps to help make this script easier for other to follow * I also updated this work to include XSS and SQL injection projection * 1- Define list of ACF fields you want to search through - do NOT include taxonomies here * See step 8 for taxonomy inclusion */ function list_searcheable_acf(){ $list_searcheable_acf = array( "your", "custom", "post-types", "here" ); 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] $search [the initial "where" part of the search query] * @param [object] $wp_query [] * @return [query-part/string] $search [the "where" part of the search query as we customized] * modified from gist: https://gist.github.com/FutureMedia/9581381/73afa809f38527d57f4213581eeae6a8e5a1340a * 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( $search, &$wp_query ) { global $wpdb; if ( empty( $search )) return $search; // 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); // 4- explode search expression to get search terms $exploded = explode( ' ', $terms ); if( $exploded === FALSE || count( $exploded ) == 0 ) $exploded = array( 0 => $terms ); // 5- reset search in order to rebuilt it as we wish $search = ''; // 6- get searcheable_acf, a list of advanced custom fields you want to search content in $list_searcheable_acf = list_searcheable_acf(); // 7- search through tags, inject into SQL search foreach( $exploded as $tag ) : $search .= " 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]): $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; else : $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; endif; endforeach; // 8- Add to search string info from comments and custom taxonomies // You would need to customize the taxonomies below to match your site $search .= ") ) 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 = 'your' OR taxonomy = 'custom' OR taxonomy = 'taxonomies' OR taxonomy = 'go' OR taxonomy = 'here' ) AND object_id = wp_posts.ID AND wp_terms.name LIKE '%$tag%' ) )"; endforeach; return $search; } // 9- use add_filter to put advanced_custom_search into the posts_search results add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); ?> -
charleslouis revised this gist
Jul 4, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,9 +12,9 @@ function 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 */ -
charleslouis revised this gist
Jul 4, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,9 +12,9 @@ function 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 */ -
charleslouis revised this gist
Jul 4, 2013 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,12 +18,12 @@ function list_searcheable_acf(){ * 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; // get search expression $terms = $wp_query->query_vars[ 's' ]; @@ -34,13 +34,13 @@ function advanced_custom_search( $search, &$wp_query ) { $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%') @@ -51,13 +51,13 @@ function advanced_custom_search( $search, &$wp_query ) { 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 @@ -80,7 +80,7 @@ function advanced_custom_search( $search, &$wp_query ) { ) )"; endforeach; return $where; } add_filter( 'posts_search', 'advanced_custom_search', 500, 2 ); -
charleslouis revised this gist
Jul 4, 2013 . 1 changed file with 13 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,23 @@ <?php /** * [list_searcheable_acf list all the custom fields we want to include in our search query] * @return [array] [list of custom fields] */ function list_searcheable_acf(){ $list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF"); 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] $search [the initial "where" part of the search query] * @param [object] $wp_query [] * @return [query-part/string] [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( $search, &$wp_query ) { global $wpdb; -
charleslouis revised this gist
Jul 4, 2013 . 1 changed file with 6 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,12 @@ <?php function list_searcheable_acf(){ $list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF"); return $list_searcheable_acf; } // see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/ // credits to Vincent Zurczak for the base query structure function advanced_custom_search( $search, &$wp_query ) { global $wpdb; @@ -62,18 +60,15 @@ function advanced_custom_search( $search, &$wp_query ) { 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 $search; } -
charleslouis created this gist
Jul 4, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ <?php function list_searcheable_acf(){ $list_searcheable_acf = array("titre_long", "sous_titre", "extrait_court", "extrait_long", "titre_tete", "titre_sequence", "sequence_phase", "sous-titre_tete", "chapeau", "introduction", "section", "team_departement", "infos_en_liste", "lien_page", "fichier"); // print_r($list_searcheable_acf); return $list_searcheable_acf; } //see also : http://wordpress.org/support/topic/include-custom-field-values-in-search // see mainly https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/ function advanced_custom_search( $search, &$wp_query ) { global $wpdb; if ( empty( $search )) return $search; // 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 $search = ''; // 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 ) : $search .= " 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]): $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; else : $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') "; endif; endforeach; $search .= ") ) 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 = 'etiquettes' OR taxonomy = 'domaine_intervention' OR taxonomy = 'category' OR taxonomy = 'prestataires' ) AND object_id = wp_posts.ID AND wp_terms.name LIKE '%$tag%' ) )"; endforeach; // print_r($search); return $search; } add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );