Popular Post by Month query not working properly
-
This is the Code I’m using… It’s working just fine to get the most popular posts of the year, but it’s still showing posts from last month (March) when we’re in April…
My website so you can check: https://newgeek.com.br/
I use it in the sidebar “Mais acessados do Mês“I think the problem is in:
‘order’ => ‘DESC&year=’ . $year . ‘&monthnum=’ . $monthBut I don’t know how to fix it…
<?php $month = date('m'); $year = date('Y'); $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC&year=' . $year . '&monthnum=' . $month ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
I’ve a code in the function that looks like this:
// Para pegar popular posts function wpb_set_post_views($postID) { $count_key = 'wpb_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); } } //To keep the count accurate, lets get rid of prefetching remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
And a “counter” in the single.php just after the Loop that looks like this:
Loop and then the code:
<?php if (have_posts() ) : while ( have_posts() ) : the_post() ?> <?php wpb_set_post_views(get_the_ID()); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Popular Post by Month query not working properly’ is closed to new replies.