Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Search by Custom field(s) WordPress

  • SOLVED

Hi,

I need a form to be able to search using the regular input text ($) but also additional field matching custom fields in my custom post type.
For example, my post type is COUNTRY, with 1 taxonomy CONTINENT and 2 custom fields SIZE & POPULATION
I need to be able to search in all combination of all fields.

Code need to be fully functional, compatible with 3.1.2

thx
s

Answers (4)

2011-05-23

Daniele Raimondi answers:

Setup a standard WP search form adding a select with all terms under CONTINENT taxonomy (see [[LINK href="http://codex.wordpress.org/Function_Reference/wp_dropdown_categories"]]wp_dropdown_categories()[[/LINK]])and 2 input fields (named size and population), one for each custom fields you wanna use in your search.
I think that for size and population you are going to make some "less,equal,greater than" search operations.

Then, in search.php, before every line of code add a piece of code like this:
<?php
global $wp_query;

$continent=mysql_real_escape_string($_GET['continent']);
$size=mysql_real_escape_string($_GET['size']);
$population=mysql_real_escape_string($_GET['population']);

$args = array(
'post_type'=> 'country',
'continent' => $continent,
);
if ( !empty( $size) && !empty( $population ) ) {
$args['meta_query']= array(
array(
'key' => 'size',
'value' => $size,
'type' => 'numeric',
'compare' => '<<use one of the available compare operations listed here http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters>>'
),
array(
'key' => 'population',
'value' => $population,
'type' => 'numeric',
'compare' => '<<use one of the available compare operations listed here http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters>>'
)
);
}
$args = array_merge( $wp_query->query, $args );
query_posts($args);
?>


And then you have to show your results with the usual WP Loop.

I've not covered all cases with search parameters, but you haven't specified your search criteria, so it is just a hint.

Hope this make sense for you!


salocine comments:

This seems to go in the right way but I will need the ability to search on individual text field only without a taxonomy selected. So $s only or $s and custom field or custom field only


Daniele Raimondi comments:

It should be just a matter of adding if-else statements here and there:

<?php

global $wp_query;

$continent=mysql_real_escape_string($_GET['continent']);
$size=mysql_real_escape_string($_GET['size']);
$population=mysql_real_escape_string($_GET['population']);

$args = array(
'post_type'=> 'country'
);

if (!empty( $continent ) ) {
$args ['continent'] = $continent;
}

if ( !empty( $size) ) {
$args['meta_query']= array(
array(
'key' => 'size',
'value' => $size,
'type' => 'numeric',
'compare' => 'use one of the available compare operations listed here http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters'
)
);
}
if ( !empty( $population ) ) {
$args['meta_query']= array(
array(
'key' => 'population',
'value' => $population,
'type' => 'numeric',
'compare' => 'use one of the available compare operations listed here http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameter'
)
);
}
$args = array_merge( $wp_query->query, $args );
query_posts($args);
?>


<strong>Remember</strong>: if your search term is <strong>empty</strong>, WP redirect your users to the homepage! So, to avoid redirection, use an input tag like this on search form:

<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" />
<div class="button">


as suggested [[LINK href="http://accessites.org/site/2009/01/wordpress-empty-searches/"]]here
[[/LINK]]


Daniele Raimondi comments:

if you wanna see a live example of exact the same situation, on this WP site

[[LINK href="http://www.real-dreams.it/"]]http://www.real-dreams.it/[[/LINK]]

made by my webagency (sorry but english is not available for now), there's a search form in home page where you can:

1) search in a custom post type (announcements)
2) search (optionally) on a custom taxonomy (named tipologia with 2 terms, boats and cars)
3) use 2 custom fields with a min and a max price for filtering the posts

it uses the same code logic described above and if you leave all fields empty, it returns all announcements (for both terms in taxonomy "tipologia", cars and boats); and you can make a search with every combination of values and fields .


salocine comments:

Thanks Daniele,
I will try this tomorrow and let you know


salocine comments:

Daniele, I cant get it to work.
I tried your website and it doesn't seem to find any result when searching by price (custom field)

Could you post the full code, including the search form and the search template with loop?


Daniele Raimondi comments:

The first field is the min price, while the second is the max price. Just insert a min value only, a max value only or a range (with min<max of course) and you should view a list of results.

Try for example a max price of 130000 and you should find 3 cars (no boats under 130000 euros, sorry ;D )

here you can find the code for the form template file

http://pastebin.com/Jctkjajk

and here the code for the serach results template file

http://pastebin.com/BJ1vu2WS

2011-05-23

derekshirk answers:

Are you looking to have more than one input field in your search form?

If you are looking to customize your search to include custom post types and custom taxonomies through a single text input search form I would recommend installing the search everything plugin:
http://wordpress.org/extend/plugins/search-everything/

2011-05-23

Julian Lannigan answers:

Hi,

I think I have a wonderful plug-in for you that will let you filter the any query based on request variables (GET, POST). It is a plug-in I just started developing on and it works at a basic level to allow filtering/searching on custom fields. I also answered a previous question almost identical to your question here.

[[LINK href="http://wpquestions.com/question/show/id/2204"]]http://wpquestions.com/question/show/id/2204[[/LINK]]

But I recommend using Wordpress to install the plug-in, just do a search for "[[LINK href="http://wordpress.org/extend/plugins/custom-query-fields/"]]Custom Query Fields[[/LINK]]" and my plug-in will be the first choice. Currently, there isn't a nice feature to determine the available search criteria, so you just have to add a single line of code for each field you want searchable to your functions.php file. Here is an example:
register_custom_queryable_field("price", array("dataType"=>"numeric"));
The above code will allow you to search on the custom field price, and since it's numeric you can do the min, max, and range search functions too. Please see the [[LINK href="http://wordpress.org/extend/plugins/custom-query-fields/"]]plug-in homepage[[/LINK]] for more information on the available options. I know the instructions are kind of crude right now, they will soon be fixed.

2011-05-24

senlin answers:

I would like to suggest to use the [[LINK href="http://www.relevanssi.com/"]]Relevanssi[[/LINK]] plugin, by default it searches Custom Fields too.