Support » Plugins » Hacks » Popular Post by Month query not working properly

  • Resolved newgeek


    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:
    I use it in the sidebar “Mais acessados do Mês

    I think the problem is in:
    ‘order’ => ‘DESC&year=’ . $year . ‘&monthnum=’ . $month

    But I don’t know how to fix it…

    		$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);
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            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)
  • Moderator bcworkz


    Everything after 'order' => 'DESC is not doing anything. The order parameter is only for ascending or descending. Secondary ordering is added to the ORDERBY directive. You are getting only a count order, date and months are not considered.

    I’m not sure if you can get WP_Query to accept the parameters the way you need them. You may need to use the ‘posts_orderby’ filter to add in the year and month parameters. This bypasses the parameter parsing which will likely be confused by your needs. This filter is applied to all queries, so you would likely need a conditional so the extra ordering is only applied to this one query.

    So no easy way around it …. I saw some people using this:

    $month = date('m');
    $year = date('Y');
    query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&monthnum=' . $month);
    while (have_posts()): the_post();

    Why does it work for this kinda of Popular post, but not for mine…?

    Anyways, any Tutorial/Guide I could follow to make mine work… Otherwise I’m giving up and just using the “popular post of all time”.

    Thanks in advance!

    Moderator bcworkz


    That snippet will not work for anyone to SORT by month and year. What that script is doing is limiting posts returned to the current month. See Class_Reference/WP_Query#Time_Parameters. It’s confusing as it is tacked on after the ORDER parameter, but they are completely independent.

    Wait a minute! Re-reading your first post, maybe I’m the one confused! Are you wishing to sort popular posts by month or only show popular posts for the current month? If only the current month, try changing the ‘m’ for the date() month parameter to ‘n’, this drops the leading zeros, which may work better in the query. This would not explain why March worked though. X/

    Sorry, but you may need to re-explain exactly what you want for idiots like me.

    No, it’s probably my fault since English is not my mother language.
    So Sorry!

    What I want is a place in the sidebar where I can show my visitors the Most Viewed Pages of the Current Month.

    Since we’re in April, I want to show my visitors the most visited/viewed posts of the current month, April. I’ve no need to sort popular posts by month, only to show the most viewed posts of the current month.

    Of course I don’t want to change the month manually everysingle month so I tried using the $month = date(‘m’); thing.

    I already tried your tip of changing it to date(‘n’); but it didn’t work. Sadly. It didn’t mess anything, but there was no change.

    And I had a post receiving a lot of views recently (an April post – 9k views ) and it went for the “second position” in the most viewed posts (so I don’t think it’s stuck in March). I think my code is just showing the “Most Viewed of All Time” when what I really want is the “Most Viewed of the Current Month“.

    By the way, thanks for taking your time and trying to help. I really appreciate this kind of thing, it’s not everyone who has this kinda of behavior.


    Moderator bcworkz


    Nothing wrong with your English. Something definitely wrong with my comprehension, it happens once in a while, and I can’t use second language as an excuse 🙁

    Anyway, I now understand your desire correctly, thank you for re-explaining. I think I see the problem as well. You are mixing the string and array form of arguments, which is probably confusing the query parser. The array form is definitely preferred. For example, &year=' . $year should be changed to 'year' => $year,

    I didn’t see this yesterday because I viewed the code in the little forum code window. Today I copied it into my programming editor and the problem jumped out in living color! I hope this fixes it, because I’m running out of ideas.

    I just want to Thank you so much! It worked and I’m pretty sure everything is fine now… All thanks to you =)

    I’m really happy because this problem was bugging me for quite some time now… Hehe

    Thanks again man!


    Moderator bcworkz


    You are most welcome! I’m pleased it turned out to be such an easy fix.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Popular Post by Month query not working properly’ is closed to new replies.