Woocommerce include custom fields in the search function
I have built a theme for a car dealership. Each car is a custom post type ("vehicle"), and has about 12 custom fields with things such as Make, Model, Mileage, Fuel Type etc. etc.
So basically on the home page I want a search form which has drop down lists for Make & Model and contains any available Makes or Models.
I also want it to have 2 options for Year, so the end user can select "2006" and "2012" and the search results contain all vehicles with the year between those two numbers.
Is there a plugin out there that can do this??
Thankyou for any help.. this has been driving me crazy for hours.
Though I’m getting more and more votes, and the solution works, but cybmeta’s answer is actually the answer that is nice and WordPress’ way. You should definitely try that.
Start by making an Advanced Search Form with which you want your user will interact with the website, and save it with a name (i.e. I saved it as advanced-searchform.php — but don’t save it with searchform.php then it’ll replace the WordPress’ default search form):
Then call the form into your template like the following:
Now your search form is ready, and you can now use the search form and can take user input into the URL.
What you just need is: query the database and query the post type and its custom fields as per the search query. Remember your search query is now the URL you got after submission of the form. Now ask WordPress to load your custom search result page when the form is submitted. Place the following function into your functions.php so that it’ll enable your custom search template instead of the default search.php :
I brought the code somewhere from WPSE (I forgot the root), but there’s controversy using the code above. But it actually works (lame excuse of course).
Check another way @G.M. suggested.
Make a new file and save it with advanced-search-result.php (because we used this name in functions.php ) and now you are free – obviously. Concept is:
- Grab the data from the URL,
- Use a simple WP_Query() (if your query is complex then use $wpdb query),
- Pass the commands within the query, fetch data from db, and
- Show the result[s]
A sample can be:
So, here is your final thing. But still there are many challenges:
- Alternative Values– an advanced search can be performed with ALLof the fields or ANYof the field, so you have to make sure the Query is taking all the results as per the search and data. You can use $wpdb custom SQL query for complex search result and that will be pure MySQL – WordPress has nothing there.
- Sanitization & Validation– the texts field and textarea are so vulnerable that can cause mal practice to your site. So passing raw data would be unsafe, you will need to Sanitize and Validate them before passing into the db query. (Data Sanitization and Validation with WordPress– TutsPlus)
- Designing– you can choose the page.php (or search.php ) template and make this page on the basis of that.
So, you got the idea, now it’s your turn to explore and discover the way. Remember, everybody’s way is different. Make yours, so that I can follow you. 🙂
Woocommerce include custom fields in the search function
I have two custom taxonomies applied to two custom post types. the terms list on the sidebar just fine and will list all posts associated with it. However, if you search one of the terms in specific, it doesn’t bring up a post with that term.
I get nothing. Any ideas? I’ve tried using various search plugins but they either break my custom search parameters or just don’t work.
I would recommend the Search Everythingplugin too, but if you want to implement this using WP’s search function, here’s the code I’m using in my Atom theme:
Is this the standard WordPress search? Because that doesn’t seem to include taxonomies (not even standard, like categories and tags) in the search. The code searches in post_title and post_content , but if you want to include anything else you should hook into the posts_search filter.
I tried the solution of Onetrickpony above https://wordpress.stackexchange.com/a/5404/37612, which is great, but I found one issue there, which did not work for me, and I would make one small modification:
- if I searched for a string in the title of the taxonomy – it works great
if the taxonomy has special characters e.g. with german "Umlauts" (ö,ä,ü) and one searches for oe, ae, ue insteda of using the special char – you need to add the search in the slug of the taxonomy – OR t.slug LIKE ‘%".get_search_query()."%’
if you search for a combination of a search query and a taxonomy filter – this also works fine
But the problem is, when you try to use only the taxonomy filter – the search hook append an empty string to the query if no text is searched for, and for that reason you get ALL posts in the result, instead of only those from the filtered taxonomy. A simple IF statement solves the problem. So the whole modified code would be this (works perfectly fine for me!)
Search WordPress by Custom Fields without a Plugin
Custom fields are one of the more powerful features available in WordPress. They are particularly useful when extending WordPress via the use of custom post types. I create custom post types all the time for things like products, portfolios, or galleries when developing WordPress themes for clients. Custom fields are extremely handy when it comes to adding product details, such as item numbers or prices. Unfortunately, it’s not possible to search WordPress by custom fields out of the box. In order to fix that, we need to modify the WordPress search query to include custom fields.
The ‘postmeta’ table is where all the custom field data is stored in the database. By default, the WordPress search functionality is set to search the ‘posts’ table only. In order to include the custom fields data in our search, we first need to perform a left join on the ‘posts’ and ‘postmeta’ tables in the database.
Modify the Query
Next we need to modify the WordPress search query to include custom fields.
Finally, we need to add the DISTINCT keyword to the SQL query in order to prevent returning duplicates.