admin管理员组文章数量:1300033
I want to add a custom fields ("introduction" and "ensavoirplus") to search of Wordpress, but the SQL code is not exact. I don't understand if i do a mistake or if WP don't can do this. But my attempt fail... I don't know why because I do exactly what the codex says.
This is my code :
function recherche_avancee( $query ) {
if ( !is_admin() && $query->is_search ) {
$custom_fields = array(
"introduction",
"en_savoir_plus_page"
);
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $_GET['s'],
'compare' => 'LIKE'
));
}
$query->set("meta_query", $meta_query);
}
}
add_action( 'pre_get_posts', 'recherche_avancee');
And this is the SQL code :
1. SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
2. FROM wp_posts
3. INNER JOIN wp_postmeta
4. ON ( wp_posts.ID = wp_postmeta.post_id )
5. WHERE 1=1
6. AND (((wp_posts.post_title LIKE '%environnement%')
7. OR (wp_posts.post_content LIKE '%environnement%')))
8. AND wp_posts.post_type IN ('post', 'page', 'attachment')
9. AND (wp_posts.post_status = 'publish'
10. OR wp_posts.post_status = 'miseenavant'
11. OR wp_posts.post_author = 3
12. AND wp_posts.post_status = 'private')
13. AND ( ( wp_postmeta.meta_key = 'introduction'
14. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%environnement%' )
15. OR ( wp_postmeta.meta_key = 'en_savoir_plus_page'
16. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%environnement%' ) )
17. GROUP BY wp_posts.ID
18. ORDER BY wp_posts.menu_order ASC
19. LIMIT 0, 10
The errors are on line 13, because I don't want a AND but a OR and the lines 13,14,15,16 should go right after line 7 that it all works.
Someone already had the same kind of error and if so where did it come from?
Thanks
I want to add a custom fields ("introduction" and "ensavoirplus") to search of Wordpress, but the SQL code is not exact. I don't understand if i do a mistake or if WP don't can do this. But my attempt fail... I don't know why because I do exactly what the codex says.
This is my code :
function recherche_avancee( $query ) {
if ( !is_admin() && $query->is_search ) {
$custom_fields = array(
"introduction",
"en_savoir_plus_page"
);
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $_GET['s'],
'compare' => 'LIKE'
));
}
$query->set("meta_query", $meta_query);
}
}
add_action( 'pre_get_posts', 'recherche_avancee');
And this is the SQL code :
1. SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
2. FROM wp_posts
3. INNER JOIN wp_postmeta
4. ON ( wp_posts.ID = wp_postmeta.post_id )
5. WHERE 1=1
6. AND (((wp_posts.post_title LIKE '%environnement%')
7. OR (wp_posts.post_content LIKE '%environnement%')))
8. AND wp_posts.post_type IN ('post', 'page', 'attachment')
9. AND (wp_posts.post_status = 'publish'
10. OR wp_posts.post_status = 'miseenavant'
11. OR wp_posts.post_author = 3
12. AND wp_posts.post_status = 'private')
13. AND ( ( wp_postmeta.meta_key = 'introduction'
14. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%environnement%' )
15. OR ( wp_postmeta.meta_key = 'en_savoir_plus_page'
16. AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%environnement%' ) )
17. GROUP BY wp_posts.ID
18. ORDER BY wp_posts.menu_order ASC
19. LIMIT 0, 10
The errors are on line 13, because I don't want a AND but a OR and the lines 13,14,15,16 should go right after line 7 that it all works.
Someone already had the same kind of error and if so where did it come from?
Thanks
Share Improve this question asked Jan 8, 2015 at 10:05 ecaLdipSecaLdipS 611 gold badge1 silver badge3 bronze badges3 Answers
Reset to default 1please never do this: 'value' => $_GET['s']
... $query->get('s');
, get_search_query, or sanitize_key($_GET['s'])
are all safer options. Technically we shouldn't be using get-params in wordpress at all, it's not a best-practice. That get value could be all sorts of bad things, & we don't want to pass it along to the db without making sure it's clean.
Also, you're using the LIKE
operator... so you might want to add quotes around the value for stricter matching.
'value' => '"'.$query->get('s').'"';
Thanks to WordPress 4.1 you can do better for meta_query : https://make.wordpress/core/2014/10/20/update-on-query-improvements-in-4-1/
function recherche_avancee($query) {
if (!is_admin() && $query->is_search) {
$meta_query = array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'introduction',
'value' => get_search_query(),
'compare' => 'LIKE',
),
array(
'key' => 'en_savoir_plus_page',
'value' => get_search_query(),
'compare' => 'LIKE',
),
),
);
$query->set('meta_query', $meta_query);
}
}
add_action('pre_get_posts', 'recherche_avancee');
Did not test but you get the idea...
I have reviewed your code but I can't find what is wrong with your code. I have an easy solution. You can use third party free WordPress plugin, WP Extended Search and select meta keys which you want to add in your search results. This plugin helps to extend WordPress default search and include post meta, taxonomies and custom post types in search results.
WP Extended Search: https://wordpress/plugins/wp-extended-search/
Hope this helps...!!
本文标签: Add custom fields to search
版权声明:本文标题:Add custom fields to search 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641242a2389929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论