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

Search with Custom Fields in search.php WordPress


hi experts,

I'm messing with search.php to use custom fields in it.
If you go to this site and see the top search box. I added a custom field "location" I need to perform the search based on 3 things.
1. keywords
2. category
3. location i.e custom field

Following is the code I'm using..

global $query_string;

I need it done ASAP.

Answers (1)


AdamGold answers:


It should work.

Muhammad Adnan comments:

Thanks for so fast reply.

I uploaded your solution to site.

I want you to make a sample search with location 'Kentucky' and ads should return all ads with that location 'Kentucky'

AdamGold comments:

Works, isn't it?

Muhammad Adnan comments:


have a look at this URL


it should only return kentucky ads .. ??

AdamGold comments:

Ok. What exactly do you want it to show? Please use Google Translate so I can understand your words.

Muhammad Adnan comments:

I want to show the posts depending on the selection criteria i.e keyword,category,custom field [location]

So, It should return only those ads depending on the custom field [location].
if keywords are empty, it should just return the results based on category and location.

I hope you got my point now ?

AdamGold comments:

Try to use this plugin:

Muhammad Adnan comments:

oh no, I don't want to use the plugin :(

there is no solution without plugin ?

AdamGold comments:

Well I guess you can, check out this tutorial:

Muhammad Adnan comments:

can u tell me why this is not working ?

AdamGold comments:

Are you sure your meta key name is cp_state? BTW please raise your prize.

Muhammad Adnan comments:

yes, it is sure cp_state.

btw, I can go max. $10 if I got what I wanted.
otherwise it is a waste :(

AdamGold comments:

It should work, what's the problem?

Muhammad Adnan comments:

go here

it should just return the 1 ad with location 'Co. Derry'

isn't it ?

AdamGold comments:

Try to print the $_GET variable and tell me its output.


Muhammad Adnan comments:

refresh the page now and you will see output

array(4) { ["s"]=> string(4) "boys" ["sa"]=> string(6) "search" ["cat"]=> string(1) "0" ["meta_value"]=> string(9) "Co. Derry" }

AdamGold comments:

That's really weird. Are you sure the meta key is cp_state and not something else?

Muhammad Adnan comments:

see attached.