Metaboxes searchable?

Support General Metaboxes searchable?Resolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #11113
    AleAle
    Participant

    Hello,

    I am wondering how can I make the contents of several meta boxes searchable by WordPress? The metaboxes are text, textarea and wysiwyg type.

    Thanks.

    #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'
            ]
        ] );
    } );
    #11125
    AleAle
    Participant

    Thanks Anh,

    I'm not sure why it isn't working but when I add the code from the tutorial to my functions.php it simply doesn't change the results at all.

    If I add teh following code, then the search is broken and I can't find anything at all.

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

    Sorry, my mistake. It is working, what is not possible is to find items in a second query taht shows the fields, I will solve it. Thanks!

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