2

I am using the following code to modify the default WordPress search and The Events Calendar search to include custom fields.

The custom fields are added by the Advanced Custom Field (ACF) plugin (https://advancedcustomfields.com/). I am particularly interested in a field which is a ACF repeater field. (Although, it would be helpful if all post_meta associated with the event post can be searched.)


/**
 * Extend WordPress search to include custom fields
 * http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
 *
 * https://theeventscalendar.com/support/forums/topic/how-to-include-custom-fields-in-search/
 */

/**
 * Join posts and postmeta tables
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

if ( is_search() || !empty( $_REQUEST['tribe-bar-search'] ) ) {
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $wpdb;

if ( is_search() || !empty( $_REQUEST['tribe-bar-search'] ) ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

if ( is_search() || !empty( $_REQUEST['tribe-bar-search'] ) ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

The code works fine for the default WordPress search but not for The Events Calendar search.

I have tried the code with the default Twenty Seventeen theme and disabling plugins other than The Events Calendar ones, to rule out any theme/plugin conflicts, but that didn't work either.

Any pointers on where I might be going wrong will be extremely helpful.

Thanks.

closed as off-topic by bueltge, Max Yudin, Jacob Peattie, shanebp, rudtek Dec 26 '18 at 15:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • I would guess that the issue lies with !empty( $_REQUEST['tribe-bar-search'] ). Try logging something in that if statement to see if your code is running when you use the tribe events search bar. It's possibly they changed their slug. – mrben522 Dec 19 '18 at 16:37
  • The if statement runs fine and the tribe-bar-search slug is correct. From - [Github] (github.com/moderntribe/the-events-calendar/blob/4.2.2/src/Tribe/…) // if a user provides a search term we want to use that in the search params if ( ! empty( $_REQUEST['tribe-bar-search'] ) ) { $query->query_vars['s'] = $_REQUEST['tribe-bar-search']; } – Hussain S M Dec 20 '18 at 6:21