I’ve searched everywhere for an answer to this and keep finding different suggestions, but none that seem to fit my particular needs. I currently have a website using a custom built search (no plugin). Website: GayBarsIn.com
The search functions I found work for searching custom post titles, post meta data (Advanced Custom Fields) and a combination of both title and post meta data, but only one post meta keyword works. I’m trying to find a way to search multiple post meta keywords.
For instance: The site shows bar listings.
- If I search for a single keyword… say “Cleveland,” it will return all listings with that keyword (whether in the title or post meta).
- If I search for “Cleveland Ohio,” it will return only those values found first in the title, then in the post meta, but just one instance of post meta. So if the title has Cleveland in it and the post meta has Ohio, it will return those results, but it will not show those that contain two instances of post meta data with keywords “Cleveland Ohio.”
What I need for it to do is show results using multiple search terms for ALL post meta (custom fields). Example: “Cleveland and Ohio” or “Nightclub and Lounge and Houston”
Here is the code I found that works okay for a single keyword search:
/* ========================================
* SEARCH CUSTOM POST TYPES
* ======================================== */
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', array('listing'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join' );
/**
* Modify the search query with posts_where
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
*/
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/(s*".$wpdb->posts.".post_titles+LIKEs*('[^']+')s*)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
/**
* Prevent duplicates
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
*/
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
About half-way down the code, this section seems to be what should be edited, but I can’t figure out how. I’ve tried switching ‘OR’ to ‘AND’ but that returns no results.
if ( is_search() ) {
$where = preg_replace(
"/(s*".$wpdb->posts.".post_titles+LIKEs*('[^']+')s*)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
I apologize if this has been asked before. I’ve looked for a few days and found others circling around what I’m trying to do, but not specifically enough for it to work for me. Any help or guidance would be much appreciated!
You should create your own form with input field for search, and catch data after post. You could split the words for example with
explode()
and put them into an array. You need to check if words exists as tag, so you need to loop through your array and check if it exists inget_tags($args)
. After that you should have an array with only useful tags. Now you can useget_posts()
);
You only need to loop (foreach) through your post-array and output results.