Unfortunately, WordPress don’t have option to measure post hits/visits, even for single post. How to get most readed/popular posts from certain category on WordPress for user defined time (ex. one day, two days, one week) we wil explain to you shortly in next steps.
- 1. Firstly, install AJAX Hits Counter + Popular Posts Widget from this link and activate it.
- 2. Install Add Categories to Pages. from this link plugin and activate it.
- 3. After that we will create new page template for our theme (ex. mostreaded.php) with code below and save/upload it to our theme folder by FTP or on other way.
<?php header('Content-type: application/json'); /** * Template Name: JSON output page * Description: JSON output page (most readed) * */ ?> <?php $categories = get_the_category(); $category_id = $categories->cat_ID; $argumenti = array( 'date_query' => array( array( 'column' => 'post_date_gmt', 'after' => '3 days ago' ) ), 'posts_per_page' => 5, 'cat' => $category_id, 'orderby' => 'meta_value_num', 'meta_key' => 'hits', 'order' => 'DESC' ); $popularno = new WP_Query($argumenti); echo "var json=". json_encode($popularno->get_posts()); if ($popularno->have_posts()): while ($popularno->have_posts()): $popularno->the_post(); endwhile; endif; wp_reset_query(); ?>
- 4. Simply create new page in your WordPress admin and assign category or categories you want, but don’t forget to assign also page template we created above. You can modify code to optimize it for your needs. Also this WP_Query you can use anywhere you want, modified or not, in your specific or default templates (post, page, custom post template…).
Also you can get more styled data instead of outputting it to JSON.
- 5. Access your newly created URL by browser and you will be able to see JSON data of most popular posts from your category/ier for specific time you previously set in page template above.