Forums › Forums › Search & Filter Pro › Is there any way to search custom meta (custom field) without Relevanssi ?
Tagged: custom field, custom meta, function.php, relevanssi, Slug
- This topic has 2 replies, 2 voices, and was last updated 5 years, 1 month ago by Anonymous.
-
Anonymous(Private) September 28, 2019 at 8:38 am #222274
Hi Trevor
I felt this is really great plugin when I tried free version. Now I purchased pro version, and I feel some important factors are different from free version and pro version.
For example, I like search slug of free version rather than pro version.
Because my webiste has code which enable to search custom meta(custom field) by the code in functions.php
Thus, free version of SF works fine because its search slug is same as normal search at my website.However, pro version changes search slug to different one, and it stopped to search custom meta(custom field).
slug of free version
mydomein/?s=searchwordslug of pro version
mydomein/?sfid=00000&_sf_s=searchwordNow, what I like to know is how I can keep regular search slug like “mydomein/?s=searchword” on pro version instead of another slug made by pro version. If its difficult, then I need to change pro version to free version. Is there any way to exclude or restrict certain category at free version?
I tried plugin Relevanssi to search custom meta(custom field), but their annual cost is much higher than SF pro and require much extra work on search.php. Also Relevanssi said he is suitable for small website only.
I like to keep my function code for custom meta(custom field) search.
Is there any way to let SF pro search like my original search function? This is code on function.php that enable search through custom meta(custom field) as original search function./**
* Extend WordPress search to include custom fields
* http://adambalee.com
*/
/**
* Join posts and postmeta tables
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
*/
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 $wpdb;
if (is_search()) {
$where = preg_replace(
“/\(\s*”.$wpdb->posts.”.post_title\s+LIKE\s*(\'[^\’]+\’)\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’); -
AuthorPosts