I want to get post meta entries in woocommerce. When user search anything from default search bar. Data should be also returned from post meta table.

So basically I need to modify the default search and make it able to get values from post meta table. I am new in Wordpress I dont even know where to make changes and what to do. I have to code no need of any plugin. I am using wordpress sixteen

Thing like adding

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', 'post');


will exclude pages from searching if we add it in functions.php I want to code like that

  • Do you want to display posts containing certain post meta (custom field) keys and values or do you want to search and display each post's custom field keys and values independently?
    – Omar Tariq
    Dec 18, 2015 at 0:58
  • Here I am suggesting you a plugin. You don't need to use it. But you can get functions from this plugin. Try extract from it.wordpress.org/plugins/relevanssi
    – Kvvaradha
    Dec 18, 2015 at 1:02
  • let user search ABC in search bar. Then search is done by checking posts and pages that contain title or content ABC. But I want that ABC should be searched in custom field too. if it is matched then it should be showen
    – Adnan Ali
    Dec 18, 2015 at 1:03
  • @Kvvaradha I want to code it in functions.php. Plugin is not requirement.
    – Adnan Ali
    Dec 18, 2015 at 1:04
  • I think I understand, I was in the same situation. Please see my answer below. @AdnanAli
    – 730wavy
    Dec 22, 2015 at 3:16

1 Answer 1


Im going to help you out here with code Im currently using on a project.

First you will need to add a query like mentioned in your question, to your theme's functions.php file. Mines looks like this --Search Query Function

If you take a look at that code, you can see I have multiple custom fields and custom taxonomies. In the code you can see the different "compare" arguments used for things like minimum price to max price, and so forth. If you dont understand it, Im sure if you study it long enough it will start to make sense to you. Just replace/remove fields.

Ok so in order for this to correlate with your search form, you will need to create a custom form with your form fields having the same name attribute as those you set in the query. So with that query my search form looks like this -- Advanced Search Form

Also the form has to have a field with "s" parameter somewhere in there. It can be hidden with no value, but on my site if I dont have it there, no posts are ever found. Here's an example of the field Im referring to, and how I have it --

 <input type="search" id="s" name="s" title="Search" placeholder="Address (optional)" class="form-control hideit" value="Search"  />

And on my search.php page, I have a normal loop --

 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <?php get_template_part('search','property'); ?>
                    <?php endwhile; endif; wp_reset_query(); ?>

Ok hopefully that should help get you in the right direction!

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.