ACF
WordPress ne recherche pas dans les champs personnalisés !
Bien qu’ACF fasse un excellent travail en utilisant les champs personnalisés de WordPress (post meta) pour le stockage de données structurées, WordPress ne recherche aucun de ces contenus !
De nombreux sites construits par WordPress qui utilisent ACF placent la grande majorité du contenu dans des champs personnalisés (et non dans l’éditeur principal) et ce, pour de bonnes raisons, cela offre une interface beaucoup plus agréable à utiliser et réduit les erreurs.
Malheureusement, un effet secondaire de cela signifie que presque aucun contenu de votre site n’est pris en compte lors de l’exécution des recherches. C’est un énorme problème !
Pour résoudre cela, nous devons modifier la requête de recherche de WordPress pour inclure les champs personnalisés.
Tout d’abord, créer le formulaire de recherche
postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'customfields_search_join' );
function customfields_search_where( $where) {
global $pagenow, $wpdb;
if( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/","(".$wpdb->posts.".post_title LIKE $1) OR
(".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter('posts_where', 'customfields_search_where' );
function customfields_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'customfields_search_distinct' );
?>
// rechercher aussi parmi les taxonomies
function customfields_taxonomies_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
// Join to the term_relationships table to access term_taxonomy_id
$join .= " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) ";
// Join to the term_taxonomy table to ensure we can filter by taxonomy
$join .= " LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) ";
// Join to the terms table to access the name of the term
$join .= " LEFT JOIN {$wpdb->terms} ON ({$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id) ";
}
return $join;
}
add_filter('posts_join', 'customfields_taxonomies_search_join');
function customfields_taxonomies_search_where( $where ) {
global $wpdb;
if ( is_search() ) {
$where .= " OR ({$wpdb->terms}.name LIKE '%".get_search_query()."%' AND {$wpdb->term_taxonomy}.taxonomy IN ('your_custom_taxonomy_here', 'another_taxonomy'))";
}
return $where;
}
add_filter('posts_where', 'customfields_taxonomies_search_where');
// You might still want to use your customfields_search_distinct function to avoid duplicate results:
// add_filter('posts_distinct', 'customfields_search_distinct');