Support General Metaboxes searchable? Reply To: Metaboxes searchable?

#11115
Anh TranAnh Tran
Keymaster

Hi Ale,

There is a nice tutorial showing how to search WordPress by all custom fields. It requires some coding and works for all text fields.

Or you can modify the query like this:

add_action( 'pre_get_posts', function( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    $query->set( 'meta_query', [
        'relation' => 'OR',
        [
            'key' => 'field_id1',
            'value' => '"' . get_search_query()  . '"',
            'compare' => 'LIKE'
        ],
        [
            'key' => 'field_id2',
            'value' => '"' . get_search_query()  . '"',
            'compare' => 'LIKE'
        ],
        [
            'key' => 'field_id2',
            'value' => '"' . get_search_query()  . '"',
            'compare' => 'LIKE'
        ]
    ] );
} );