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.
1 Answer
Afaik, there’s no relation
key for meta_query
. Use compare
instead and maybe also specify a type
to speed things up with skipping types that are out of scope.
EDIT
$args = array(
'post_type' => 'product',
'meta_query' => array(
// Used to set two meta keys IN RELATION TO EACH OTHER
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
// Used to FILTER OUT/IN/LIKE THE VALUE NAME
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );