Search only custom fields
-
How can i search only custom fields?
code on my single.php
<?php $values = get_post_custom_values("Genre"); echo $values[0] ?>
Example: Drama, Thriller, WarNow i wish something like:
mywebsite.com/?genre=Actioni tested this script on functions.php
/** * Join posts and postmeta tables * * https://codex.www.ads-software.com/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 * * https://codex.www.ads-software.com/Plugin_API/Filter_Reference/posts_where */ function cf_search_where( $where ) { global $pagenow, $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 * * https://codex.www.ads-software.com/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' );
I tried:
mywebsite.com/?s=Actionbut it’s confused. appears too many titles and contents !!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Search only custom fields’ is closed to new replies.