• Resolved asfarfordev

    (@asfarfordev)


    Please how to get most popular in 1 hour with WP_Query

    $popularpost = new WP_Query( array( 
         'post_type' =>'news',
      'posts_per_page' => '3',
    'meta_key'=>'views_total',
    'orderby'=>'meta_value_num', 
    'order' => 'DESC'  ) );

    thanks

Viewing 1 replies (of 1 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @asfarfordev,

    Assuming you’re following the How To: Sorting a custom query by views (All time, monthly, weekly, or daily) tutorial then you need to add one more custom field for posts to track their hourly views data, like so for example:

    update_post_meta(
        $postid,
        'views_hourly',
        wpp_get_views(
            $postid,
            [
                'range' => 'custom',
                'time_unit' => 'hour',
                'time_quantity' => 1
            ],
            false
        )
    );

    Then change your WP_Query code to this:

    $popularpost = new WP_Query( array( 
        'post_type' => 'news',
        'posts_per_page' => 3,
        'meta_key'=> 'views_hourly',
        'orderby'=> 'meta_value_num', 
        'order' => 'DESC'
    ) );

    See wpp_get_views() for more details.

Viewing 1 replies (of 1 total)
  • The topic ‘how to get most popular in 1 hour with WP_Query’ is closed to new replies.