Orderby Post Views

You can sort your query by view number, using the meta key :

$query = new WP_Query( 
            array( 
                'meta_key' => 'post_views_count',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
            ) 
        );

This will query your posts based on the views. You can change DESC to ASC or RAND to achieve what you asked.

To make it work in action, you can use pre_get_posts() to filter your query. Use this piece code in your theme’s functions.php file:

add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
    if ( 
        !is_admin() && 
        $query->is_main_query() && 
        ( $query->is_home() || $query->is_archive() || $query->is_search() )
    ) {
        if (isset($_REQUEST['orderby'])) {
            $order = $_REQUEST['orderby'];
        }
        if ( $order === 'views') {
            $query->set('meta_key', 'post_views_count');
            $query->set('orderby', 'meta_value_num');
            $query->set('order', 'DESC');
        }
    }
}

Now, whenever you visit http://example.com/?orderby=views, your posts will be filtered by the number of views ( descending, you can change it to anything you want )

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)