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

Custom Tag Search WordPress

  • REFUNDED

I am looking for a simple custom tag search form.

Here is an example of what I am aiming for but I need help actually getting it to function properly.

<form method="get" action="<?php bloginfo('url'); ?>">
<fieldset>
<input value="enter search terms..." onfocus="if (this.value == 'enter search terms...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'enter search terms...';}" class="search-text" name="s" type="text">
<span>Location:</span>
<select name="tag">
<option value="">Any Location</option>
<option value="america">America</option>
</select>
<span>Cuisine:</span>
<select name="tag">
<option value="">Any Cuisine</option>
<option value="american">American</option>
</select>
<button type="submit">Search</button>
</fieldset>
</form>


Basically it's Search Term (Optional) + Tag (optional) + Tag.

Answers (2)

2018-04-20

Reigel Gallarde answers:

Location and Cuisine are tags?


twdesigns comments:

blank selectors to tell people which to select. So they would be null if nothing selected for that drop down. They would pull the drop down, down, called Location and pick America as in the example above.

Hope that makes sense!


Reigel Gallarde comments:

yes, but are these tag or taxonomy(category) in wordpress sense?


Reigel Gallarde comments:

If I get you right, and I assume it's a tag in wordpress, then I suggest you change the name attribute of your select.

for location,
<select name="location">
for cuisine,
<select name="cuisine">

and use this code

add_filter( 'query_vars', 'add_query_vars_filter' );
function add_query_vars_filter( $vars ){
$vars[] = "location";
$vars[] = "cuisine";
return $vars;
}

add_action('pre_get_posts', 'pre_get_posts');

function pre_get_posts($query) {

if ( !$query->is_admin && $query->is_search) {
$location = get_query_var('location', '');
$cuisine = get_query_var('cuisine', '');
$tags = array();
if ( !empty( $location ) ) {
$tags[] = $location;
}
if ( !empty( $cuisine ) ) {
$tags[] = $cuisine;
}
$tag = implode('+', $tags); // use '+' if location and cuisine, Display posts that have "all" of these tags
//$tag = implode(',', $tags); // use ',' if location and cuisine, Display posts that have "either" of these tags
echo $tag;
if ( !empty( $tag ) ) {
$query->set('tag', $tag );
}

}

return $query;
}


Reigel Gallarde comments:

please remove the echo $tag; on my code... it's there for debugging purpose..


twdesigns comments:

Made the changes for location and cuisine.
Added the code to my functions.

On a search it returns the following URL structure but just loads the home page instead of results.
https://xxxxxxxxxxxxxxxxxxxx.com/?location=america&cuisine=bbq-restaurants

Suggestions?


Reigel Gallarde comments:

you should have a page for your search result.
A blog page can also be a search page.
If your home page is not a blog page, it will not display any result.


twdesigns comments:

I have a search.php in my template which a normal search uses. I'm not sure I understand what else is required for the query to complete correctly?

Thanks


Reigel Gallarde comments:

Your "action" attribute in the form should point on a page where posts are listed. Usually it's a blog page. And on default theme has homepage as a blog page.

Do you have a Posts page?
Or can you show as the link of your website you're working so I could check?

You can also create a Template and create a page using this Template.
And then your action in your form should point on this newly created page.


twdesigns comments:

www.vh58.com leads to a site with a link that will take you directly to the page with your code in the search area top.


twdesigns comments:

If it's easier to do with without the search area were they type in, I don't need them to type. Just select 2 tags for search.


twdesigns comments:

Do you have this working somewhere? your echo shows correctly when I query but results fail.

2018-04-20

Arnav Joy answers:

Do you have exiting site?


twdesigns comments:

See above link pls