• Resolved yonatanlanger

    (@yonatanlanger)


    Hi everyone, I understood the free search of the plugin is based on wordpress search and it is not considering taxonomies and authors, but only post title, post content, and?excerpt?fields.

    Does anyone have experience in extending the search to taxonomies and the author field?

    I was hoping its possible to create a Code Snippet and make it work.

    Feedback much appreicated.

    Yonatan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter yonatanlanger

    (@yonatanlanger)

    No one needed a free search for taxonomies or authors before?

    Thread Starter yonatanlanger

    (@yonatanlanger)

    I manage to adjust the wordpress search to search also in author, but the filter everything search still doesnt work, any ideas?

    // Register the custom query variable ‘srch’
    function register_custom_query_vars( $vars ) {
    $vars[] = ‘srch’; // Add ‘srch’ to query vars
    return $vars;
    }
    add_filter( ‘query_vars’, ‘register_custom_query_vars’ );

    // Modify the search query to include ‘srch’ and author metadata
    function custom_library_author_search( $search, $query ) {
    global $wpdb;

    // Check if it's the main query, a search query, and for the custom post type "library"
    if ( !is_admin() && $query->is_search() && $query->get('post_type') === 'library' ) {
    
        // Debug: Output query variables
        if ( defined('WP_DEBUG') && WP_DEBUG ) {
            error_log(print_r($query->query_vars, true));
        }
    
        // Get the search term from 'srch' (Filter Everything Pro) or fallback to 's'
        $search_term = $query->get('srch') ? $query->get('srch') : $query->get('s');
    
        if ( !empty($search_term) ) {
            // Escape the search term for safety
            $search_term = esc_sql( $search_term );
    
            // Modify the search query to include author names
            $search = " AND (
                {$wpdb->posts}.post_title LIKE '%{$search_term}%'
                OR {$wpdb->posts}.post_content LIKE '%{$search_term}%'
                OR {$wpdb->users}.display_name LIKE '%{$search_term}%'
            )";
    
            // Add a join to the users table to include author names
            add_filter( 'posts_join', function( $join ) use ( $wpdb, $query ) {
                if ( $query->get('post_type') === 'library' ) {
                    return $join . " LEFT JOIN {$wpdb->users} ON {$wpdb->posts}.post_author = {$wpdb->users}.ID ";
                }
                return $join;
            });
    
            // Ensure the DISTINCT clause is added to avoid duplicate results
            add_filter( 'posts_distinct', function( $distinct ) use ( $query ) {
                if ( $query->get('post_type') === 'library' ) {
                    return "DISTINCT";
                }
                return $distinct;
            });
        }
    }
    
    return $search;

    }
    add_filter( ‘posts_search’, ‘custom_library_author_search’, 10, 2 );

    // Optional: Flush rewrite rules on activation for ‘srch’
    function custom_flush_rewrite_rules() {
    flush_rewrite_rules();
    }
    add_action( ‘init’, ‘custom_flush_rewrite_rules’ );

    Plugin Support fesupportteam

    (@fesupportteam)

    Hi @yonatanlanger

    Please check in the plugin how the SKU search was applied, it should help you write the correct code.

    Best Regards – Victor

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