Forum Replies Created

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

    (@yonatanlanger)

    Hi Victor, it somehow worked now. Thank you anyways

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

    Thread Starter yonatanlanger

    (@yonatanlanger)

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

Viewing 3 replies - 1 through 3 (of 3 total)