1

Hi I'm using this code below to display the most popular post on my wordpress site. It works great! I was wondering if there was anyway to show the most popular post of the current day?

This in my functions.php

function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
}
else{
    $count++;
    update_post_meta($postID, $count_key, $count);
}
}

and this in my theme file

<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<li><?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID());; ?></li>
<?php endforeach; ?>
8
  • That would require you to save the counts in a separate meta with today's date in the key, then select. example 'post_views_count_' . date('d-m-Y') and when updating post_views_count also update this one. Guess now that would be easier to do the rest..
    – Ismail
    Jun 15, 2016 at 4:35
  • I'm not completely understanding what you are saying @SamuelElh Jun 15, 2016 at 4:44
  • Let me write you some code to try out.. That would be easier than these tiny comments.
    – Ismail
    Jun 15, 2016 at 4:47
  • I would appreciate that @SamuelElh Jun 15, 2016 at 5:04
  • see this pastebin.com/DBA8remL (Please read the code comments)
    – Ismail
    Jun 15, 2016 at 5:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.