Complex Search functionality. Advice needed

There’s a few ways you could do this. I once wrote some extended code to integrate Apache Solr with WordPress search and BuddyPress and that added a lot of power. With something like Apache Solr you can customize weight and do some interesting things. Solr is a full fledged search engine and you will need to run a server for it (or purchase a cloud based instance). Check out the Solr for WordPress plugin as a starting place if that seems like a good route for you. The biggest negative here is that you have to run Solr and you’ll need to learn a bit about it before it will have the power that you need. You will be amazed however at the speed and complexity that Solr can do.

The next most powerful way to accomplish what you’re looking for is writing your own search query functions using the $wpdb object. This is not an easy option but the most likely to produce the results that you are looking for. The other caveat with this option is that you’ll have to keep an eye on changes to the WordPress MySQL structure since it may affect your queries. Start with something like this and then you’ll need to research MySQL joins in order to get all the queries the way that you want:

global $wpdb;
$query = $wpdb->prepare("
  SELECT *
  FROM $wpdb->posts
  WHERE post_title LIKE %s
", $search); 

Another more limited solution is that you could use add_filter(‘posts_search’, ‘my_custom_search_function’) to build a filter that changed the posts results search as you desired but this may not give you everything you need/want.

As a final solution you could look into the wp-types.com plugin Views and see if it’s query building function would work for you. I’d guess that a custom taxonomoy and/or custom fields (via the Types plugin) might be able to accomplish your goals here.

Leave a Comment