admin管理员组

文章数量:1425142

My client has a WP website, its a real estate site with the properties setup as custom post types. Below is my search query to filter the listings based on a search form, all fairly standard stuff.

These pages are taking near 10 seconds to load and i cannot determine why, i have had a plugin enabled in the past that showed the queries themselves as taking around 100ms to run.

The site is fast generally, until you do a search, then it slows to a crawl

Until recently i thought it was just wordpress, its known to be slow in general but i bought a theme recently thats wordpress based and is a real estate management theme, its absolutely lightning fast compared to my clients site.

Its on a dedicated server, high end Xeon and is fairly low traffic, i don't think its hardware related as that other theme is fast on the same server.

What can i do to speed this up or properly debug it?

Any insight would be incredibly helpful, thanks

function search_listings( $query ) {

//* if within admin, return
if ( is_admin() ) {
    return;
}
//* if not main query, return
else if ( ! $query->is_main_query() ) {
    return;
}
else if ( $query->is_main_query() && (is_post_type_archive( 'sales_listings' ) || is_post_type_archive( 'long_term_rentals' ) || is_post_type_archive( 'listings' ))) {
    if( (isset($_GET['price-min']) && $_GET['price-max'] != '')         || 
        (isset($_GET['price-max']) && $_GET['price-max'] != '')         ||
        (isset($_GET['beach']) && $_GET['beach'] != '')                 ||
        (isset($_GET['beds-min']) && $_GET['beds-min'] != '')           ||
        (isset($_GET['baths-min']) && $_GET['baths-min'] != '')         ||
        (isset($_GET['keyword']) && $_GET['keyword'] != '')             ||
        (isset($_GET['property_type']) && $_GET['property_type'] != '') ||
        (isset($_GET['location']) && $_GET['location'] != '')           ||
        (isset($_GET['confidential']) && $_GET['confidential'] != '')   ||
        (isset($_GET['dock']) && $_GET['dock'] != '')) {

        if(isset($_GET['price-min']) && $_GET['price-min'] != '') {
            $min_price  = sanitize_text_field($_GET['price-min']);
            $search_array['price-min'] = array( 'key'       => 'price',
                                                'value_num' => intval($min_price),
                                                'type'      => 'numeric',
                                                'compare'   => '>='
                                            );
        }
        if(isset($_GET['price-max']) && $_GET['price-max'] != '') {
            $max_price  = sanitize_text_field($_GET['price-max']);
            $search_array['price-max'] = array( 'key'       => 'price',
                                                'value' => intval($max_price),
                                                'type'      => 'numeric',
                                                'compare'   => '<='
                                            );
        }
        if(isset($_GET['beds-min']) && $_GET['beds-min'] != '') {
            $beds_min   = sanitize_text_field($_GET['beds-min']);
            $search_array['beds-min'] = array(  'key'       => 'beds',
                                                'value'     => intval($beds_min),
                                                'type'      => 'numeric',
                                                'compare'   => '>='
                                            );
        }
        if(isset($_GET['baths-min']) && $_GET['baths-min'] != '') {
            $baths_min  = sanitize_text_field($_GET['baths-min']);
            $search_array['baths-min'] = array( 'key'       => 'baths',
                                                'value'     => intval($baths_min),
                                                'type'      => 'numeric',
                                                'compare'   => '>='
                                            );
        }
        if(isset($_GET['confidential']) && $_GET['confidential'] == 'yes') {
            $search_array['private_collection'] = array(    'key'       => 'private_collection',
                                                            'value'     => 1,
                                                            'type'      => 'numeric',
                                                            'compare'   => '='
                                            );
        }
        if(isset($_GET['dock']) && $_GET['dock'] == 'yes') {
            $search_array['private_collection'] = array(    'key'       => 'dock',
                                                            'value'     => 1,
                                                            'type'      => 'numeric',
                                                            'compare'   => '='
                                            );
        }
        $keyword        = (isset($_GET['keyword']) && $_GET['keyword'] != '') ? sanitize_text_field($_GET['keyword']): '';
        if($keyword != '') {
            $query->set('s',$keyword);
        }
        if(isset($_GET['property_type']) && $_GET['property_type'] != '') {
            /*$property_type_array = "";
            foreach($_GET['property_type'] AS $key => $value) {
                // echo $value;
                $property_type_array[] = sanitize_text_field($value);
            }*/
            $property_type_array = sanitize_text_field($_GET['property_type']);
            $search_array['property_type'] = array( 'key'       => 'property_type',
                                                    'value'     => $property_type_array,
                                                    'compare'   => 'IN'
                                            );
        }
        if(isset($_GET['location']) && $_GET['location'] != '') {
            /*foreach($_GET['location'] AS $key => $value) {
                // echo $value;
                $location_array[] = sanitize_text_field($value);
            }*/
            $location_array = sanitize_text_field($_GET['location']);
            $search_array['location'] = array(  'key'       => 'location',
                                                'value'     => $location_array,
                                                'compare'   => '='
                                            );
        }
        if(isset($_GET['beach']) && $_GET['beach'] != '') {

            $beach = sanitize_text_field($_GET['beach']);
            $search_array['beach'] = array( 'key'       => 'beach',
                                            'value'     => $beach,
                                            'compare'   => '='
                                            );
        }
        $query->set('posts_per_page', 30);
        if(is_post_type_archive( 'sales_listings' ))
        {
            $query->set('post_type', array('sales_listings'));
        }
        else if(is_post_type_archive('long_term_rentals' ))
        {
            $query->set('post_type'     , 'long_term_rentals');
        }
        else if(is_post_type_archive('listings' ))
        {
            // $query->set('post_type'  , array('sales_listings','long_term_rentals'));
            $query->set('post_type' , 'listings');
        }

        if(!empty($search_array)) {
            $query->set('meta_query'    , $search_array);   
        }
        $query->set('meta_key'      , 'price');
        $query->set('orderby'       , 'meta_value_num');
        $query->set('order'         , 'DESC');
    }   
    else {
        if(is_post_type_archive( 'sales_listings' ))
        {
            $query->set('post_type'     , 'sales_listings');
        }
        else if(is_post_type_archive('long_term_rentals' ))
        {
            $query->set('post_type'     , 'long_term_rentals');
        }
        else if(is_post_type_archive('listings' ))
        {
            $query->set('post_type'     , array('sales_listings','long_term_rentals'));
        }

        $query->set('posts_per_page', 30);
        $query->set('meta_key'      , 'price');
        $query->set('orderby'       , 'meta_value_num');
        // $query->set('post_type'      , 'sales_listings');
        $query->set('order'         , 'DESC');      


    } 
}

}

本文标签: custom post typesWhy is this function so slow