Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Adding Two Search Forms

Hello, I've been at this all day, trying to understand how to go about it, but I'm not sure what files to use, what code to put in where, etc., and this is a bit more complex. The Wordpress videos have helped so much, and I wanted to extend my functionality just a bit further than what the videos showed.

I have a basic search form created by copying over the code located in search.php from the default twentyeleven template, and then using the code <?php get_search_form(); ?> on the page where I want the form to be. So far, so good! However, here is where things start to get tricky:

1: I want to EXCLUDE a custom post type I made called "schools" from my search inquiry. I don't want the search to look for those custom post types, but every other kind of post on the site.

2: I wanted to include a second search form on the site on my "schools" page. I know how to create a searchpage.php file, which I learned from reading the Codex, but I have no idea what to put inside that page. (I read from an old support post that I can have two search pages, and use two search forms. This was read here: http://wordpress.org/support/topic/include-custom-field-values-in-search ). The search form I wanted on this page is to search for under the custom post type "schools", as well as search through an additional custom field type, "locations".

So when a user, for example, types in "School 1", the search will attempt to find under the custom post type labeled "schools", any of the posts of "School 1". If the user types in, "Orlando", posts with the field "location" filled out as "Orlando" will show up; this is also filtered under the custom post type "schools".

I'm not sure how to refine my search query for those results, and what file to put what code, and how to make sure it works properly. I've been reading forum posts on Wordpress.org, which gave me several ideas of how to go about it, but no specific context to help me.

This could also be a neat video to include, for sure. I feel so close yet so far to the solution everytime I research. Thanks for the help - love the videos, and I couldn't get this far without it!

5 Answers

It has been 5 days. I don't want/like to bump, but I think 5 days is a lot of time to bump and see if anyone can please help me with a solution. Thanks, guys.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Ricardo Rodriguez! Apologies for the delay, we have a 4 day work week here some sometimes posts that come in on Thursdays or Fridays might not get answered till Monday.

Try this great post from WP Beginner to see if it helps: http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/

Thank you Zac, the code is working well, but there's still a few tweaks I want.

You can view the quick mock-up here: http://praxart.com/testdb/schools/

The issues I'm having are the following:

1: If you type in any of the custom fields into the search bar - for example, if you type in "Sarasota" or any location for the school - you don't get any results. That's a problem; I want the search to look at specific custom fields, as well.

2: When I do get results, I want it to display different information and formatting on the results page; how do I go about formatting that?

So very close, I can feel it! Just needs to pull from custom fields and have a slightly different format for the results. The code is so helpful, though, so thank you!

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

I am not too sure about the customization needed to search the custom fields in with your search, but there is a plugin that offers that functionality. I haven't used it, but you should check it out and let us know what you think: http://wordpress.org/plugins/wp-custom-fields-search/

As for formatting the result, you would create a search.php (maybe copy one from the twentytwelve theme to see what it looks like) and customize that. You can have to add some conditional logic so it knows which format to use.

Hope that helps!