Problem with search with Custom Fields

Home Forums Calendar Products Filter Bar Problem with search with Custom Fields

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1321889
    Joachim
    Participant

    Hey,

    I use Event Calendar Pro and Filter Bar.
    I’m having a problem with Custom Fields added to the search results.
    I read and followed this topic : https://theeventscalendar.com/support/forums/topic/how-to-include-custom-fields-in-search/

    I can now find my stuff in the event list in the WordPress Administration, but not on my website through the tribe-bar-search.

    Here is the code I pasted in my theme’s functions.php :

    /**
     * Extend WordPress search to include custom fields
     *
     * http://adambalee.com
     */
    
    /**
     * Join posts and postmeta tables
     *
     * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
     */
    function cf_search_join( $join ) {
        global $wpdb;
    
    /* https://theeventscalendar.com/support/forums/topic/how-to-include-custom-fields-in-search/ */
    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;
       
    /* https://theeventscalendar.com/support/forums/topic/how-to-include-custom-fields-in-search/ */
    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;
    
    /* https://theeventscalendar.com/support/forums/topic/how-to-include-custom-fields-in-search/ */
    if ( is_search() || !empty( $_REQUEST['tribe-bar-search'] ) ) {
            return "DISTINCT";
        }
    
        return $where;
    }
    add_filter( 'posts_distinct', 'cf_search_distinct' );
    #1322649
    Shelby
    Participant

    Hi Joachim,

    Unfortunately, at this time, it’s not a built in option, except for additional fields added according to this article, but this change could be made with some customization. I recommend checking out the following resources if you’d like to try making these changes yourself or to find someone to help out with this:

    TEC Settings Overview (To check out the available options in the plugin)
    Themer’s Guide (Help for making the changes yourself)
    Customization Guide (Help finding help with making custom changes)

    Best,
    Shelby 🙂

    #1332865
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Problem with search with Custom Fields’ is closed to new replies.