1

Right now I can search only through title and content.

How can I search also through a custom meta box value?

1 Answer 1

1

It depends. There are plugins that can add this functionality, I have used SearchWP in the past, it works well with WooCommerce.

You could also write your own custom function and add it to your theme's functions.php file. Something like this should work.

function my_custom_search( $query ) {
    if( ! is_admin() && $query->is_main_query() ) {
        if ( $query->is_search() ) { 

            $meta_query = array(
                'key'       => 'custom_meta',
                'value'     => $query->query['s'],
                'compare'   => 'LIKE'  
            );

            $query->set('meta_query', $meta_query);

        }
    }
}

add_action('pre_get_posts' , 'my_custom_search');
3
  • Is this for the wp-admin screen? Here /wp-admin/edit.php?post_type=product ? Commented Sep 11, 2018 at 12:54
  • My bad, I misread your question. Try this plugin I just tried it and it works well in the admin. Go into the setting and check to allow custom field meta. Commented Sep 11, 2018 at 13:52
  • it works well in the frontend, but in the wp-admin it doesn't work. Commented Sep 11, 2018 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.