Custom Fields
Re-Envisioned
Support General Metaboxes searchable?

This topic contains 4 replies, has 2 voices, and was last updated by  Ale 9 months, 3 weeks ago.

  • Creator
    Topic
  • #11113
    Resolved
    Ale
    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #11115

    Anh 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

    Ale
    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

    Ale
    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 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.