woocommerce offtopic - update default search function and get postmeta entries

admin2025-04-18  0

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');
    }
  }
}

add_action('pre_get_posts','search_filter');

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

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');
    }
  }
}

add_action('pre_get_posts','search_filter');

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

Share Improve this question edited Dec 21, 2015 at 21:38 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Dec 17, 2015 at 23:43 Adnan AliAdnan Ali 1171 silver badge7 bronze badges 5
  • 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 Commented 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/plugins/relevanssi – Kvvaradha Commented 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 Commented Dec 18, 2015 at 1:03
  • @Kvvaradha I want to code it in functions.php. Plugin is not requirement. – Adnan Ali Commented Dec 18, 2015 at 1:04
  • I think I understand, I was in the same situation. Please see my answer below. @AdnanAli – 730wavy Commented Dec 22, 2015 at 3:16
Add a comment  | 

1 Answer 1

Reset to default 0

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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744949841a276265.html

最新回复(0)