6

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

1

please 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').'"';
1

Thanks to WordPress 4.1 you can do better for meta_query : https://make.wordpress.org/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...

1
  • Thanks for your response, the first order relation "OR" does not change the "AND" (before the first meta_key) in the SQL code ... I saw the different change from WP 4.1 in order meta_query I feel this doesn't fix my problem: /
    – ecaLdipS
    Jan 9 '15 at 10:28
-1

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.org/plugins/wp-extended-search/

enter image description here

Hope this helps...!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.