WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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(
    'Immeuble' => array(
    'Programme neuf' => array(
        'programme neuf'
    'Maison, Villa' => array(
        utf8_encode( 'propriété' )
    'Fond de commerce' => array(
        'fond de commerce'
    'Terrain' => array(

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.


Not really an answer but several clues to optimize my query.

share|improve this question
up vote 0 down vote accepted

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.


$args = array(
    'post_type' => 'product',
    'meta_query' => array(
            // Used to set two meta keys IN RELATION TO EACH OTHER
        'relation' => 'OR',
            'key' => 'color',
            'value' => 'blue',
                    // Used to FILTER OUT/IN/LIKE THE VALUE NAME
            'compare' => 'NOT LIKE'
            'key' => 'price',
            'value' => array( 20, 100 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
 $query = new WP_Query( $args );
share|improve this answer
There's an example in the codex with the relation key for meta_query. Check the very last example here : codex.wordpress.org/Class_Reference/… – Kaaviar Mar 16 '12 at 9:50
You're using it completely wrong. First: That's only for two meta_query arrays that you want to set in relation to each other. Second: That should be placed outside the array - as seen in the codex. See Edit with the codex example. – kaiser Mar 16 '12 at 9:59
Thanks for the information but... First: it's not explicitly written its limited to two meta_query arrays. Second: that's exactly what I did - 'relation' is a key of 'meta_query' in my example too. I'm just populating meta_keys and meta_values dynamicaly later in the script. – Kaaviar Mar 16 '12 at 10:10
Large Meta queries are indeed performance killers. There's some ideas to reduce this, but basically if you're asking for a lot of SQL work, it's gonna take a lot of CPU to do it. There is no current fix, short of a custom query. – Otto Mar 17 '12 at 1:54
I made a ticket on this (for tax_query) a while back. It's a slightly complex problem to make a smarter query that works for all cases: core.trac.wordpress.org/ticket/18105 – Otto Mar 17 '12 at 14:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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