• Hello everyone.

    I’m creating a website for a real estate company and to allow people to search for houses and flats, there is a search engine with this fields : maximum price, minimum area, typology, locations, delivery date and type of dwelling.

    I use Custom Post Type UI and Advanced Custom Fields PRO.

    Here is my code for the query :

    $prix = !empty($_POST['prix']) ? $_POST['prix'] : 9999999;
    $superficie = !empty($_POST['superficie']) ? $_POST['superficie'] : 0;
    $type_logement = !empty($_POST['type_logement']) ? $_POST['type_logement'] : '';
    $typologie = !empty($_POST['typologie']) ? $_POST['typologie'] : '';
    $lieux = !empty($_POST['lieux']) ? $_POST['lieux'] : '';
    $date = !empty($_POST['date']) ? $_POST['date'] : '';
    
    if(!empty($_POST)) {
        $args = array(
            'numberposts'   => -1,
            'post_type'     => array('bien-immobilier'),
            'post_status'   => 'publish',
            'fields'        => 'ids',
            'orderby'       => 'date',
            'order'         => 'DESC',
            'meta_query'    => [
                'relation'  => 'AND',
                array(
                    'key' => 'type_logement',
                    'value' => $type_logement,
                    'compare' => 'LIKE',
                ),
                array(
                    'key' => 'typologie',
                    'value' => $typologie,
                    'compare' => 'LIKE',
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'lieu',
                            'value' => $lieux,
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key' => 'departement',
                            'value' => $lieux,
                            'compare' => 'LIKE',
                        ),
                    ),
                ),
                array(
                    'key' => 'prix',
                    'value' => $prix,
                    'type' => 'NUMERIC',
                    'compare' => '<='
                ),
                array(
                    'key' => 'superficie',
                    'value' => $superficie,
                    'type' => 'NUMERIC',
                    'compare' => '>='
                ),
                array(
                    'key' => 'annee_livraison',
                    'value' => $date,
                    'type' => 'NUMERIC',
                    'compare' => '<='
                ),
            ],
        );
        $produits = new WP_Query($args);
    }else{
        $args = array(
            'posts_per_page'   => 12,
            'post_type'     => array('bien-immobilier'),
            'post_status'   => 'publish',
            'orderby'       => 'date',
            'fields'        => 'ids',
            'order'         => 'DESC'
        );
        $produits = new WP_Query($args);
    }

    I don’t understand why this request is very slow (10-15 seconds). There are like 30 products in total for the moment in database. And if I just comment the price (“Prix”) part, it’s faster.

    It’s not the display of products that is slow, because even when I comment everything except the query, it’s still slow.

    Could you help me, please ? Thanks !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Meta queries are terribly inefficient. The more complex the meta query, the worse it gets. Why price in particular? IDK. Maybe the numeric conversion. All meta data is stored as varchar, essentially a string that can be interpreted as other types.

    If complex meta queries are a common need, a different schema is called for, in the way of a custom table where each meta key gets its own column. The problem is the existing custom field plugin doesn’t work that way. Maybe it could be adapted, but I’m skeptical. Even if possible, it’d be akin to major surgery coding-wise.

    Because a table like I describe would be unique to your data schema, I doubt there’s an existing plugin that works that way. It likely calls for a custom coded solution.

    Thread Starter unsautenavant

    (@unsautenavant)

    Ok, thanks for your reply. So I guess it will stay this way, as I’m more of a front dev. Do you thing a better web host could improve the loading speed ? I can say that to my client…

    Moderator bcworkz

    (@bcworkz)

    It’s possible better hardware or a less trafficked server would yield faster queries, but not guaranteed. Or the improvement may not be very significant. The nature of such queries are going to make them relatively slow no matter what.

    Thread Starter unsautenavant

    (@unsautenavant)

    Okay…such a shame that a so popular tools like WordPress and ACF are this slow. It’s not like my request was very complicated…

    Thanks for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Very slow WP_Query request’ is closed to new replies.