Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. felthy revised this gist Apr 30, 2017. 1 changed file with 76 additions and 82 deletions.
    158 changes: 76 additions & 82 deletions custom-search-acf-wordpress.php
    Original 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
    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;
    }

    /*
    * [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 )) {
    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_raw = $wp_query->query_vars[ 's' ];

    // 2- check search term for XSS attacks
    $terms_xss_cleared = strip_tags($terms_raw);
    $terms = $wp_query->query_vars['s'];

    // 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 );
    // 2- explode search expression to get search terms
    $exploded = preg_split('|\s+|', $terms);
    if ($exploded === FALSE || count($exploded) == 0) {
    $exploded = array(0 => $terms);
    }

    // 5- setup search variable as a string
    // 3- setup search variable as a string
    $search = '';
    $params = array();

    // 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 ) {
    // 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 (
    (".$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
    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 ".$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
    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 '%$tag%')" );
    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 '%$tag%')" );
    } else {
    array_push($metaStatements, "(meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%s')");
    $params []= $s;
    }
    }
    $search .= join( $metaStatements, "\n OR " );
    $search .= join($metaStatements, "\n OR ");
    $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
    ) " .
    // 6- Adds to $search DB data from comments
    "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;
    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

    // 12- use add_filter to put advanced_custom_search into the posts_search results
    // 8- use add_filter to put advanced_custom_search into the posts_search results
    add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
  2. @fiskhandlarn fiskhandlarn revised this gist Dec 9, 2016. 1 changed file with 63 additions and 56 deletions.
    119 changes: 63 additions & 56 deletions custom-search-acf-wordpress.php
    Original 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]
    ##############################
    ########### 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;
    $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;
    global $wpdb;

    if ( empty( $search )) {
    return $search;
    }
    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);

    // 1- get search expression
    $terms_raw = $wp_query->query_vars[ 's' ];
    // 3- do another check for SQL injection, use WP esc_sql
    $terms = esc_sql($terms_xss_cleared);

    // 2- check search term for XSS attacks
    $terms_xss_cleared = strip_tags($terms_raw);
    // 4- explode search expression to get search terms
    $exploded = explode( ' ', $terms );
    if( $exploded === FALSE || count( $exploded ) == 0 ) {
    $exploded = array( 0 => $terms );
    }

    // 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 = '';

    // 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();

    // 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;

    // 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 .= "
    // 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 (
    "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%') ";
    }
    // 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 .= ")
    }
    $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 (
    "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 (
    // 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 $search
    } // closes foreach
    return $search;
    return $search;
    } // closes function advanced_custom_search

    // 12- use add_filter to put advanced_custom_search into the posts_search results
  3. @fiskhandlarn fiskhandlarn revised this gist Dec 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions custom-search-acf-wordpress.php
    Original 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
  4. @fiskhandlarn fiskhandlarn revised this gist Dec 9, 2016. 1 changed file with 15 additions and 18 deletions.
    33 changes: 15 additions & 18 deletions custom-search-acf-wordpress.php
    Original 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 (
    "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%') ";
    $search .= " (meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%$tag%') ";
    } else {
    $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
    $search .= " OR (meta_key = '" . $searcheable_acf . "' AND meta_value LIKE '%$tag%') ";
    }
    }
    $search .= ")
    )

    )".
    // 10- Adds to $search DB data from comments
    OR EXISTS (
    "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 (
    "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

    ON ".$table_prefix."term_relationships.term_taxonomy_id = ".$table_prefix."term_taxonomy.term_taxonomy_id".
    // 11b- Add custom taxonomies here
    WHERE (
    "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 $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 );
    ?>
  5. @jserrao jserrao revised this gist Jul 27, 2016. 1 changed file with 95 additions and 90 deletions.
    185 changes: 95 additions & 90 deletions custom-search-acf-wordpress.php
    Original file line number Diff line number Diff line change
    @@ -1,115 +1,120 @@
    /*
    ##############################
    ########### Search ###########
    ##############################
    ##############################
    ########### 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]
    */

    /**
    *
    * [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
    */
    // 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"
    );
    "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;
    * 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);

    if ( empty( $search )) {
    return $search;
    }
    // 3- do another check for SQL injection, use WP esc_sql
    $terms = esc_sql($terms_xss_cleared);

    // 1- get search expression
    $terms_raw = $wp_query->query_vars[ 's' ];
    // 4- explode search expression to get search terms
    $exploded = explode( ' ', $terms );
    if( $exploded === FALSE || count( $exploded ) == 0 ) {
    $exploded = array( 0 => $terms );
    }

    // 2- check search term for XSS attacks
    $terms_xss_cleared = strip_tags($terms_raw);
    // 5- setup search variable as a string
    $search = '';

    // 3- do another check for SQL injection, use WP esc_sql
    $terms = esc_sql($terms_xss_cleared);
    // 6- get searcheable_acf, a list of advanced custom fields you want to search content in
    $list_searcheable_acf = list_searcheable_acf();

    // 4- explode search expression to get search terms
    $exploded = explode( ' ', $terms );
    if( $exploded === FALSE || count( $exploded ) == 0 ) {
    $exploded = array( 0 => $terms );
    }
    // 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%')

    // 5- setup search variable as a string
    $search = '';
    // 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%'
    )

    // 6- get searcheable_acf, a list of advanced custom fields you want to search content in
    $list_searcheable_acf = list_searcheable_acf();
    // 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

    // 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;
    }
    // 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

    // 11- use add_filter to put advanced_custom_search into the posts_search results
    // 12- use add_filter to put advanced_custom_search into the posts_search results
    add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
    ?>
  6. @jserrao jserrao revised this gist May 18, 2016. 1 changed file with 49 additions and 46 deletions.
    95 changes: 49 additions & 46 deletions custom-search-acf-wordpress.php
    Original 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- search through tags, inject each into SQL query
    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 (";
    // 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 (
    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 = 'here'
    )
    AND object_id = wp_posts.ID
    AND wp_terms.name LIKE '%$tag%'
    )
    )";
    }
    return $search;
    // 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;
    }

    // 9- use add_filter to put advanced_custom_search into the posts_search results
    // 11- use add_filter to put advanced_custom_search into the posts_search results
    add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
    ?>
  7. @jserrao jserrao revised this gist Mar 13, 2016. 1 changed file with 23 additions and 21 deletions.
    44 changes: 23 additions & 21 deletions custom-search-acf-wordpress.php
    Original 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;
    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 );
    if( $exploded === FALSE || count( $exploded ) == 0 ) {
    $exploded = array( 0 => $terms );
    }

    // 5- reset search in order to rebuilt it as we wish
    // 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 into SQL search
    foreach( $exploded as $tag ) :
    // 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 (";
    foreach ($list_searcheable_acf as $searcheable_acf) :
    if ($searcheable_acf == $list_searcheable_acf[0]):
    // 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 :
    } 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
    }
    }
    // 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 = 'go'
    OR taxonomy = 'here'
    )
    taxonomy = 'your'
    OR taxonomy = 'custom'
    OR taxonomy = 'taxonomies'
    OR taxonomy = 'here'
    )
    AND object_id = wp_posts.ID
    AND wp_terms.name LIKE '%$tag%'
    )
    )";
    endforeach;
    }
    return $search;
    }

  8. @jserrao jserrao revised this gist Mar 11, 2016. No changes.
  9. @jserrao jserrao revised this gist Mar 11, 2016. 1 changed file with 91 additions and 67 deletions.
    158 changes: 91 additions & 67 deletions custom-search-acf-wordpress.php
    Original file line number Diff line number Diff line change
    @@ -1,86 +1,110 @@
    <?php
    /*
    ##############################
    ########### 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("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF");
    $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] $where [the initial "where" part of the search query]
    * @param [query-part/string] $search [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]
    * @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( $where, &$wp_query ) {
    function advanced_custom_search( $search, &$wp_query ) {
    global $wpdb;

    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();
    if ( empty( $search ))
    return $search;

    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 (";
    // 1- get search expression
    $terms_raw = $wp_query->query_vars[ 's' ];

    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;
    // 2- check search term for XSS attacks
    $terms_xss_cleared = strip_tags($terms_raw);

    $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 );
    // 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 );
    ?>
  10. @charleslouis charleslouis revised this gist Jul 4, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions custom-search-acf-wordpress.php
    Original 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]
    * @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
    */
  11. @charleslouis charleslouis revised this gist Jul 4, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions custom-search-acf-wordpress.php
    Original 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] $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]
    * @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
    */
  12. @charleslouis charleslouis revised this gist Jul 4, 2013. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions custom-search-acf-wordpress.php
    Original 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( $search, &$wp_query ) {
    function advanced_custom_search( $where, &$wp_query ) {

    global $wpdb;

    if ( empty( $search ))
    return $search;
    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
    $search = '';
    $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 ) :
    $search .= "
    $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]):
    $search .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
    $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
    else :
    $search .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
    $where .= " OR (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value LIKE '%$tag%') ";
    endif;
    endforeach;

    $search .= ")
    $where .= ")
    )
    OR EXISTS (
    SELECT * FROM wp_comments
    @@ -80,7 +80,7 @@ function advanced_custom_search( $search, &$wp_query ) {
    )
    )";
    endforeach;
    return $search;
    return $where;
    }

    add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
  13. @charleslouis charleslouis revised this gist Jul 4, 2013. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions custom-search-acf-wordpress.php
    Original 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;
    }

    // see https://vzurczak.wordpress.com/2013/06/15/extend-the-default-wordpress-search/
    // credits to Vincent Zurczak for the base query structure

    /**
    * [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;
  14. @charleslouis charleslouis revised this gist Jul 4, 2013. 1 changed file with 6 additions and 11 deletions.
    17 changes: 6 additions & 11 deletions custom-search-acf-wordpress.php
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,12 @@
    <?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);
    $list_searcheable_acf = array("title", "sub_title", "excerpt_short", "excerpt_long", "xyz", "myACF");
    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/
    // 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 = 'etiquettes'
    OR taxonomy = 'domaine_intervention'
    OR taxonomy = 'category'
    OR taxonomy = 'prestataires'
    taxonomy = 'post_tag'
    OR taxonomy = 'category'
    OR taxonomy = 'myCustomTax'
    )
    AND object_id = wp_posts.ID
    AND wp_terms.name LIKE '%$tag%'
    )
    )";
    endforeach;
    // print_r($search);
    return $search;
    }

  15. @charleslouis charleslouis created this gist Jul 4, 2013.
    80 changes: 80 additions & 0 deletions custom-search-acf-wordpress.php
    Original 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 );