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

  • SOLVED

hi experts,

I'm messing with search.php to use custom fields in it.
If you go to this site http://elook.ie/ 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;
query_posts($query_string."&paged=$paged&meta_value=".$_GET['meta_value']."&orderby=title&order=ASC");


I need it done ASAP.
THanks

Answers (1)

2011-04-01

AdamGold answers:

query_posts($query_string."&paged=$paged&meta_key=location&meta_value=".$_GET['meta_value']."&orderby=title&order=ASC");

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:

hmm,

have a look at this URL
http://elook.ie/?s=boys&sa=search&cat=0&meta_value=Kentucky

s=boys
meta_value=Kentucky

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:
http://wordpress.org/extend/plugins/wp-custom-fields-search/


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:
http://tobymackenzie.wordpress.com/2009/11/18/stearns-wordpress-custom-queries/


Muhammad Adnan comments:

can u tell me why this is not working ?
query_posts($query_string."&paged=$paged&meta_key=cp_state&meta_value=".$_GET['meta_value']."&orderby=title&order=ASC");


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
http://elook.ie/?s=boys&sa=search&cat=0&meta_value=Co.%20Derry

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.

var_dump($_GET);


Muhammad Adnan comments:

refresh the page now and you will see output

http://elook.ie/?s=boys&sa=search&cat=0&meta_value=Co.%20Derry

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.