• Resolved marcuslovedogs

    (@marcuslovedogs)


    Hi, I’m using the filters to filter by custom fields on my site. Is it possible to do the same with the search bar please? My custom post types are made up of numerous CFs but currently I can only search the titles.

    I can set the default Elementor Search widget to do this on a separate page, but would love to just have it work alongside the filter widget on one page.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP Smart Widgets

    (@nomade123456)

    Hi @marcuslovedogs,

    Yes, it’s possible. Right now, the BPFWE search widget only use the default search ‘s’ argument from WordPress, so it can only looks at post titles and content.

    If you want the search bar to also look in your custom fields, you have two options:

    1. Use the filter widget test input:

    In your filter widget, go to “Data Source > Custom Field”, then “Field Type > Input Field”. This allows users to search your custom fields directly through the filter widget.

    2. Use a Query ID with the filter widget:

    Assign a unique Query ID to your filter widget under “Additional Options”. Then add the following to your child theme’s functions.php:

    // Replace 'my_custom_filter' with your filter widget's Query ID
    add_filter( 'bpfwe/filter_query_args/my_custom_filter', function( $args, $widget ) {
        
        // Get the search term from POST
        $search = ! empty( $_POST['search_query'] ) ? sanitize_text_field( wp_unslash( $_POST['search_query'] ) ) : '';
    
        if ( $search ) {
            $custom_fields = array( 'field_1', 'field_2', 'field_3' ); // change for your own ACF keys
            $meta_query = array( 'relation' => 'OR' );
    
            foreach ( $custom_fields as $field ) {
                $meta_query[] = [
                    'key'     => $field,
                    'value'   => $search,
                    'compare' => 'LIKE',
                ];
            }
    
            // Merge with existing meta queries if any
            if ( isset( $args['meta_query'] ) ) {
                $args['meta_query'] = array_merge( $args['meta_query'], $meta_query );
            } else {
                $args['meta_query'] = $meta_query;
            }
        }
    
        return $args;
    }, 10, 2 );

    This setup will make the search bar looks into your custom fields as well.

    If you want, I can also write a version if you’d like this to run without a filter widget on the same page. Let me know if any of those solutions work for you.

    Regards,
    Dara

    Thread Starter marcuslovedogs

    (@marcuslovedogs)

    Thanks for that, I got it working in the child theme (using PODS) eventually but it needed one more line

    // Clear ‘s’ to prevent title/content filtering overriding meta matches
    $args[‘s’] = ”;

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

The topic ‘Search by Custom Fields’ is closed to new replies.