Support » Plugin: The Events Calendar » Extend The Events Calendar search to include custom fields

  • Resolved chugs

    (@chugs)


    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.

    • This topic was modified 3 months ago by  chugs.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Ed

    (@erishel)

    Hey there,

    Thanks for reaching out 😄

    This is not within the scope of free support that we provide here, but I will leave this open in case anyone from the community wants to chime in.

    Take care,
    Ed 🤟

    Plugin Support Ed

    (@erishel)

    Hey there,

    Since this topic has been inactive for a while, I’m going to go ahead and mark as resolved. Don’t hesitate to reopen or create a new topic if you still need help! 😄

    Take care,
    Ed 🤟

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.