admin管理员组文章数量:1122832
I have 2 postmeta english_title and english_content for each post. When user search something, plus post tile and post content, I need it search on this two meta keys too. I use this function
function custom_search_query( $query ) {
// Ensure it is a search query and the main query
if ( $query->is_search() && $query->is_main_query() ) {
// Get the search term
$search_terms = $query->query_vars['s'];
// Add meta query for searching custom fields
$meta_query = [
'relation' => 'OR',
[
'key' => 'english_title',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
[
'key' => 'english_content',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
];
// Set the meta query for custom fields
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'custom_search_query' );
I am using them by default with no plugin, but when I use this function, nothing shows up, even if the keyword is in the post title. there is nothing on the debug file.
could you tell me how can debug it to solve the problem
I have 2 postmeta english_title and english_content for each post. When user search something, plus post tile and post content, I need it search on this two meta keys too. I use this function
function custom_search_query( $query ) {
// Ensure it is a search query and the main query
if ( $query->is_search() && $query->is_main_query() ) {
// Get the search term
$search_terms = $query->query_vars['s'];
// Add meta query for searching custom fields
$meta_query = [
'relation' => 'OR',
[
'key' => 'english_title',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
[
'key' => 'english_content',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
];
// Set the meta query for custom fields
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'custom_search_query' );
I am using them by default with no plugin, but when I use this function, nothing shows up, even if the keyword is in the post title. there is nothing on the debug file.
could you tell me how can debug it to solve the problem
Share Improve this question edited Sep 18, 2024 at 21:25 Chris Cox 2,2661 gold badge13 silver badges22 bronze badges asked Sep 18, 2024 at 16:47 StudentStudent 32 bronze badges1 Answer
Reset to default 0For WP_Query()
, you don't need to add the %
before and after the keyword when using LIKE
as comparison. WP_Query()
will handle it for you. If you add %
, WP_Query()
thinks that you want to add that character itself to the search. That's the first reason you don't see any results.
The second problem is that if you have the query as above, the $search_term
need to match (post title OR post content) AND (meta field english_title OR english_content).
You can use $wpdb()
to search instead of WP_Query()
because sometimes WP_Query()
is limited for customisation like that.
Please follow the tutorial given here: https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
本文标签: post metaAdd specific postmeta to search query
版权声明:本文标题:post meta - Add specific postmeta to search query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291022a1928636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论