function get_nb_offres( $typeOffre ) {
global $type_bien_correspondances;
$args = array(
'post_type' => 'annonces',
'meta_query' => array(
'relation' => 'OR'
)
);
foreach( $type_bien_correspondances[$typeOffre] as $typeBien ) {
$args['meta_query'][] = array(
'key' => 'type_de_bien',
'value' => $typeBien
);
}
$annonces = new WP_Query( $args );
return $annonces->post_count;
}
My global $type_bien_correspondances is here :
$type_bien_correspondances = array(
'Appartement' => array(
'studios',
'T1',
'T2',
'T3',
'T4',
'T5'
),
'Immeuble' => array(
'immeuble'
),
'Programme neuf' => array(
'programme neuf'
),
'Maison, Villa' => array(
'maison',
'villa',
utf8_encode( 'propriété' )
),
'Fond de commerce' => array(
'fond de commerce'
),
'Terrain' => array(
'terrain'
)
);
And finally my call to the get_nb_offres() function is here :
// Within a function
return get_nb_offres( 'Appartement' );
My problem is when running this code my server CPU goes crazy and I can’t do anything but restart it. Commenting the ‘relation’ line make the code work but it’s not what I’m expecting.
I can still muddle through this problem running several WP_Query but I would prefer to understand where the bug comes from.
EDIT
Not really an answer but several clues to optimize my query.
Afaik, there’s no
relation
key formeta_query
. Usecompare
instead and maybe also specify atype
to speed things up with skipping types that are out of scope.EDIT