Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
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.

share|improve this question
add comment

1 Answer

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.

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 );
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
1  
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
1  
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
2  
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
show 4 more comments

Your Answer

 
discard

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.