Add custom fields to search

Question

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

0
, , , ecaLdipS 3 months 0 Answers 61 views 0

Leave an answer