Skip to content

Instantly share code, notes, and snippets.

@XavierRheem
Forked from wking-io/acf_custom_search.php
Created January 31, 2024 19:55
Show Gist options
  • Select an option

  • Save XavierRheem/e56e390cf1e564bb5720b0a44fa011cb to your computer and use it in GitHub Desktop.

Select an option

Save XavierRheem/e56e390cf1e564bb5720b0a44fa011cb to your computer and use it in GitHub Desktop.

Revisions

  1. @wking-io wking-io revised this gist Oct 2, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions acf_custom_search.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    <?php
    /**
    * [list_searcheable_acf list all the custom fields we want to include in our search query]
    * This could easily be something that is added to the admin as a plugin so that users can
    * add searchable fields in an options table
    * @return [array] [list of custom fields]
    */
    function list_searcheable_acf(){
  2. @wking-io wking-io revised this gist Sep 28, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions searchform.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <form id="searchform" action="http://localhost:8888/ltc" method="get">
    <input class="inlineSearch" type="text" name="s" value="Enter a keyword" onblur="if (this.value == '') {this.value = 'Enter a keyword';}" onfocus="if (this.value == 'Enter a keyword') {this.value = '';}" />
    <form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="text" name="s" value="Enter a keyword" placeholder="Search" />
    <!-- This hidden field will default your search to be only post type you want -->
    <input type="hidden" name="post_type" value="Your Post Type" />
    <input class="inlineSubmit" id="searchsubmit" type="submit" alt="Search" value="Search" />
    <input id="searchsubmit" type="submit" alt="Search" value="Search" />
    </form>
  3. @wking-io wking-io revised this gist Sep 28, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions searchform.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <form id="searchform" action="http://localhost:8888/ltc" method="get">
    <input class="inlineSearch" type="text" name="s" value="Enter a keyword" onblur="if (this.value == '') {this.value = 'Enter a keyword';}" onfocus="if (this.value == 'Enter a keyword') {this.value = '';}" />
    <!-- This hidden field will default your search to be only post type you want -->
    <input type="hidden" name="post_type" value="Your Post Type" />
    <input class="inlineSubmit" id="searchsubmit" type="submit" alt="Search" value="Search" />
    </form>
  4. @wking-io wking-io created this gist Sep 28, 2017.
    79 changes: 79 additions & 0 deletions acf_custom_search.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    <?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] $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;

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