You could possibly use query_posts() – http://codex.ww.wp.xz.cn/Function_Reference/query_posts, but I’d use a custom query.
I’m assuming you want the results to be displayed on your index page and that you want only the Post Title to be searched? If so, add this to the top of your index.php page –
global $wpdb;
$posts = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM '.$wpdb->posts.'
WHERE post_title="$_GET['s']"
OR post_title="$_GET['+']"
'));
Thread Starter
Arash
(@john25)
Thank you, but how can I implement that in the dropdown box?
The dropdown I made give the following
?&s=dog&s=white
instead of
?&s=dog+white
It’s probably because you are using the _GET method rather than the _POST method – I’d advise changing that and then amending the code I gave to this –
global $wpdb;
$posts = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM '.$wpdb->posts.'
WHERE post_title="$_POST['s']"
OR post_title="$_POST['+']"
'));
To be honest, I’d also recommend changing the name field of your <select> boxes, as ‘s’ and ‘+’ don’t really mean much. You can literally name them what you want.
Thread Starter
Arash
(@john25)
It gives a unexpected T_STRING error. So I changed it into
global $wpdb;
$posts = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM '.$wpdb->posts.'
WHERE post_title="$_POST["s"]"
OR post_title="$_POST["+"]"
'));
Sorry, that’s me typing badly – should be this…
global $wpdb;
$posts = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM '.$wpdb->posts.'
WHERE post_title="'.$_POST['s'].'"
OR post_title="'.$_POST['+'].'"
'));
Thread Starter
Arash
(@john25)
Thank you.
The search results are even less accurate and it is still
?&s=dog&s=white
instead of
?&s=dog+white
Ok, I see what you are looking to happen, but I’ve gone in a slightly different direction. The _POST method is much more scalable than the _GET method, so I’d abandon that. Change your original code that you posted to –
<form method="post" id="searchform" action="<?php bloginfo('siteurl'); ?>">
<div>
<label class="screen-reader-text" for="s">Search for:</label>
Type
<select name='field1' id='field1' class='postform' >
<option value='0' selected='selected'>All</option>
<option class="level-0" value="dog">dog</option>
<option class="level-0" value="cat">cat</option>
</select>
Colour
<select name='field2' id='field2' class='postform' >
<option value='0' selected='selected'>All</option>
<option class="level-0" value="white">white</option>
<option class="level-0" value="black">black</option>
</select>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Then, as a starter, add this to the top of your index page (to make sure the correct results are getting through –
echo '<pre>';
print_r($_POST);
echo '</pre>';
If you have the correct options that you selected for your search in the _POST array, then replace the above code with the code I supplied earlier –
global $wpdb;
$posts = $wpdb->get_results($wpdb->prepare(
'SELECT * FROM '.$wpdb->posts.'
WHERE post_title="'.$_POST['field1'].'"
OR post_title="'.$_POST['field2'].'"
'));
Thread Starter
Arash
(@john25)
Thank you I am going to try that and let you know the outcome.
Thread Starter
Arash
(@john25)
The above code does not work at all. π
I think there is miscommunications. WordPress has it’s own search function and there is an option to search more than one word.
So all I want to do is have 2 rows of preselected search terms.
http://www.site.com/?&s=row1+row2
Thread Starter
Arash
(@john25)
I think something like str_replace should be able to fix it. I am doing some experiments with that.
Thread Starter
Arash
(@john25)
Anyone have a suggestion? What seems to be a very basic things turns out to be very complicated. I cannot find anyone who has had a similar issue and string replacement does not seem to work with 2 search terms.