admin管理员组文章数量:1398807
It's a little hard to explain but I suppose you can understand if I elaborate it even more...
Normally when you search on a WordPress site, the results are either based on Title, Content, Category, Tags.
I want my Search to go through posts and look at custom fields too... So if a user searches for a specific custom field, let's say I have the post titled "Blah" and I created a field name is "Chocolate" and the field content is "Cadbury" so when user Search's for "Cadbury" the post "Blah" Should show up... I know there are few plugins for that like </>
but I'd be better off not bloating my site with too many plugins and plugins for every little thing...
I am not so great with WordPress but I suppose wp_query may do the work... Than you for any and every help
It's a little hard to explain but I suppose you can understand if I elaborate it even more...
Normally when you search on a WordPress site, the results are either based on Title, Content, Category, Tags.
I want my Search to go through posts and look at custom fields too... So if a user searches for a specific custom field, let's say I have the post titled "Blah" and I created a field name is "Chocolate" and the field content is "Cadbury" so when user Search's for "Cadbury" the post "Blah" Should show up... I know there are few plugins for that like <http://wordpress/plugins/relevanssi/>
but I'd be better off not bloating my site with too many plugins and plugins for every little thing...
I am not so great with WordPress but I suppose wp_query may do the work... Than you for any and every help
Share Improve this question edited May 1, 2018 at 18:21 Mostafa Soufi 8057 silver badges19 bronze badges asked Apr 27, 2018 at 2:43 Edogawa KudoEdogawa Kudo 1173 silver badges9 bronze badges 1- 1 Actually Relevanssi may be a better choice here rather than trying to code something yourself. Native custom field queries are very slow, and trying to search all custom fields will probably grind your server to a halt. – Milo Commented Apr 27, 2018 at 3:26
2 Answers
Reset to default 1There are few ways to solve this problem...
1. Don't bother and use Relevansi (or some other similar plugin)
You can often hear that using many plugins is a bad thing. It's not entirely true. It's bad only if these plugins have big impact on use of resources. In this case, Relevansi won't cause such impact.
Of course there is another problem with plugins - you have to maintain them (updates, and so on).
2. Use posts_join and posts_where hooks
It's the easiest way of solving your problem. Just join the posts and postmeta tables and add WHERE clauses that will search your value in proper custom fields.
// Join with postmeta table
function mycf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .= " LEFT JOIN {$wpdb->postmeta} mycfpm ON ({$wpdb->posts}.ID = mycfpm.post_id ";
}
return $join;
}
add_filter('posts_join', 'mycf_search_join' );
// Add where clauses
function mycf_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 (mycfpm.meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'mycf_search_where' );
// Prevent duplicates
function cf_search_distinct( $distinct ) {
global $wpdb;
if ( is_search() ) {
return 'DISTINCT';
}
return $distinct;
}
add_filter( 'posts_distinct', 'mycf_search_distinct' );
The only problem with this solution is that it can be a little bit slow. Especially if you don't want to search in all custom fields.
3. Custom CF indexing
You can also use save_post
hook to create something like an index. After saving post you get all custom fields that should be searchable, combine their values and save as another custom field.
So let's say there is a post with CFs: color=red
, location=europe
, name=john
and all of them should be searchable. You can get these values, separate them with some separator char/sequence (for example #, so you'll get red#europe#john
) and save this string as new CF (let's call it _cf_search_string).
function mycf_prepare_search_index_cf( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) return;
$meta_keys = array('cf_key1', 'cf_key2', 'cf_key3');
$search_string = '';
foreach ( $meta_keys as $k ) {
$search_string .= get_post_meta( $post_id, $k, true ) . '#';
}
update_post_meta( $post_id, '_cf_search_string', $search_string );
}
add_action( 'save_post', 'mycf_prepare_search_index_cf', 100, 1 );
Then you can use code from 2. to search only in this one custom field and not all of them, so it will add only one join and won't multiply rows that will be searched during DB query.
To make your custom fields searchable, by adding them as meta_query
-s into a WP_Query()
may also do the job:
$args = array(
'post_type' => 'your_custom_post_type', // for example
'meta_query' => array(
'relation' => 'AND', //so you can add any number of arrays = CF fields
array(
'key' => 'Chocolate', //acf meta field name
'value' => $searched_term, //e.g.:"Cadbury"
'compare' => 'LIKE',
),
array(
'key' => 'Your_another_custom_field',
'value' => $searched_term2,
'compare' => 'LIKE',
),
),
);
//then running the query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
<!-- your code in the loop -->
}
More details on the official Wordpress dev docs page about WP_Query.
本文标签: Search Posts with Custom Fields as query
版权声明:本文标题:Search Posts with Custom Fields as query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650513a2617654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论