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

Searching/Filtering Posts by Custom Fields

Background: I know a fair amount of WordPress but I am still learning PHP and jQuery, so a lot of the advanced things I could do with it are still out of reach. However, I'm learning as much as possible to get to the point where I can do these things.

I want to search or filter WordPress posts based on custom fields. I've Googled it to death and the prevailing wisdom is that it can't be done.

There is one plugin that seems to solve it (http://wordpress.org/plugins/wp-custom-fields-search) but I can't get it to work and the guy no longer supports it. The plugin code is way beyond my ability, so I can't pick it apart and figure out why it isn't working for me.

My custom fields are pretty simple: Just some dropdowns made with the Advanced Custom Fields plugin. I want to be able to search for posts based on the options in the dropdowns.

Questions:

1) Why is it so hard to search this way? (It seems like it would be easy, because custom fields are just extra columns in the wp_posts database.)

2) Can it be done, and how?

Hoping someone can help me out here because I'm stumped.

2 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I apologize for the delay in this response:

(1) Custom fields are actually in a different table, and they are laid out a different level of granularity. (Each custom field is a separate row in the postmeta table; they are not a column in the posts table.)

(2) It can be done. I use the Relevanssi plugin: [http://wordpress.org/plugins/relevanssi/]. It provides an interface for specifying which custom fields get included in the search.

Does that help?

Thanks, Randy. I appreciate the reply. I wasn't aware that custom fields were stored in a different table so this is something for me to dig into. :)

I'm actually looking at using wp_query to create a custom fields filter now. I tried the Relevanssi plugin but the problem was not so much including custom fields in search results, but searching only custom fields (like filtering products on Amazon, versus using the search bar). I think wp_query will do the job.

Thanks again!