WP_Query orderby meta_value Speed

Question

I currently sorting my posts using this code:

function main_query($query) {
    if ( is_search() || is_home() || is_front_page() || is_archive() ) {
        if (!is_admin() && $query->is_main_query()) {
            $query->set('post_status', 'publish');
            if(isset($_GET['s'])) {
                $query->set('post_type', 'post');
            }
            if(isset($_GET['orderby']) && $_GET['orderby']=="date") {
                $query->set('orderby', 'publish_date');
                $query->set('order', 'DESC');
            } else {
                $query->set('meta_key', 'ranking');
                $query->set('orderby', 'meta_value_num');
                $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
            }
        }
    }
}
add_action('pre_get_posts', 'main_query');

so by default it orders posts by their ranking meta_key value, is this a good approach or do you think is better to create a “ranking” mysql table to use it as index only to order the posts? please note that there will be more than 200k posts… Thanks

0
, , , , Damian 8 months 0 Answers 118 views 0

Leave an answer