WordPress.org

Forums

Post Views Count
[resolved] Help migrating from WP-Postviews to your plugin (15 posts)

  1. oomskaap
    Member
    Posted 2 years ago #

    It seems your plugin can support date ranges, something other plugins cannot. So i would like to query most viewed post from today or a week.
    Currently i call my posts via 2 ways.

    One is

    <?
    query_posts('category_name=asdf&showposts=8&meta_key=views&orderby=meta_value_num&order=DESC&monthnum=' . date( 'n', current_time('timestamp')));
    ?>

    But i'm not sure how to make this "daily" instead of monthly.

    Second way i can use is:

    <?php
        $cat_id='-8';//the category ID
    $limit = get_option('posts_per_page');
    query_posts(array('category__and'=>array(276),
    'showposts'=>8,'more' => $more = 0,
    'meta_key' => 'views',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    ));
    ?>

    I know i simply have to replace 'meta_key' => 'views', with 'meta_key' => '_count-views_all'

    But, what i dont know and really need is to include a date range. The above code outputs total views from all time. Tomorrow, i would like to have a new list of posts instead of the total view's posts showing again.

    http://wordpress.org/extend/plugins/baw-post-views-count/

  2. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    Hello

    The plugin does not support date ranges but dates, just dates like "a day" or "a month", not "from X to Y".
    But you can get posts most viewed last week, it is ok ?
    If yes do this :

    $yw = date('W')-1 > 0 ? date('Y') . date('W')-1 : date('Y')-1 . '52';
    ...
    'meta_key' => '_count-views_week-' . $yw;
    ...

    Try this ;)

  3. oomskaap
    Member
    Posted 2 years ago #

    Getting the most viewed last week would be great.

    Can you please help me to make sure the code is correct, i tried the below, it gives no error, but shows no posts

    <?php
        $cat_id='-8';//the category ID
    $limit = get_option('posts_per_page');
    $yw = date('W')-1 > 0 ? date('Y') . date('W')-1 : date('Y')-1 . '52';
    query_posts(array('category__and'=>array(276),
    'showposts'=>8,'more' => $more = 0,
    'meta_key' => '_count-views_week-' . $yw,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    ));
    ?>

    Do i have to put something in functions.php also?

  4. oomskaap
    Member
    Posted 2 years ago #

    .... i just realized something. Since it only gets post from LAST WEEK, it means it won't show anything right now. Since i only installed you plugin today. Am i right?

  5. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    Do you have posts viewed last week ?

  6. oomskaap
    Member
    Posted 2 years ago #

    No, i only installed your plugin today. But i do have views for last week, but just from the other plugin WP-Post views, which uses "views" as a meta key and not _count-views_week-. Any way to make views count instead of _count-views_week-?

  7. oomskaap
    Member
    Posted 2 years ago #

    Just to test, I manually created a custom field count-views_week- and made the value 10. But the above command still brings up no posts.

    When i remove the .$yw, part from 'meta_key' => '_count-views_week-' . $yw, , the posts shows correctly. So maybe it's something to do with that.

  8. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    Yes you can hook "views" in place of my own meta name.
    And like you said, if you just installed it today, last week is empty ;)
    So, to hack "views" name do this :

    add_filter( 'baw_count_views_meta_key', 'baw_count_views_meta_key', 10, 2 );
    function baw_count_views_meta_key( $time, $date )
    {
        $yw = date('W')-1 > 0 ? date('Y') . date('W')-1 : date('Y')-1 . '52';
        if( $time == 'week' && $date == $ym )
            return 'views';
    }

    I check if the called time is 'week' with date 'last week' (kind of 201238). Then, if yes, i hack the '_count-views_week-201238' in 'views'.
    By the way, i just realize that if someone use this (like you) the real count (mine) won't be updated, so i'll update the plugin right now, then, next week, the count of the last week (are you still following me ? ^^) will be ok.

    Stay tuned

  9. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    Version 2.19.2 updated.
    Try it, thx !

  10. oomskaap
    Member
    Posted 2 years ago #

    Thanks so much for all your time!

    I thought if i create a _count-views_week- meta key and fill it with a value, i could use it as a test, but it shows empty.

    So i'm not sure how to test it other than waiting one week.

  11. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    "_count-views_week-" but you missed the week, you have to ass the YEAR and the WEEK number.
    I think we are about week #38, and i'm pretty sure we are in 2012.
    "_count-views_week-201238"
    Ok ?

  12. oomskaap
    Member
    Posted 2 years ago #

    I saw what you meant before. I checked my database and i see:

    meta_key

    _count-views_week-201237

    meta_value

    5

    Wouldn't there be so many meta keys created for each date?
    Is it possible to make one meta key, "week" , and then each week your plugin can clear that value, but leaves the "all" and other values. This way you only have one meta key for week, and each week its fresh, giving u exactly what u wanted, per week views.

    Now you must manually add the week number to your template file. If it can be dynamic automatic then it would be nice.

  13. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    You can hack it yes :)
    My plugin is pretty flexible, do this :

    add_filter( 'baw_count_views_meta_key', 'baw_count_views_meta_key', 10, 2 );
    function baw_count_views_meta_key( $time, $date )
    {
        if( $time == 'week' )
            return 'week';
    }

    Take care, this code overwrite the "views" hack. In this code i'm just checking if the $time is "week" then a return "week" only, not week-201237.
    Also, if you do not need "year", "day" etc you can do this to avoid multi meta key creation :

    add_filter( 'baw_count_views_timings', 'baw_count_views_timings' );
    function baw_count_views_timings( $timings )
    {
        return array( 'all'=>'', 'week'=>'YW' ); // Keep only "all" and "week"
    }

    Flexible i said ... ;)

  14. oomskaap
    Member
    Posted 2 years ago #

    Your modifications work and i can see it being created in the database.
    The only problem i think is with the code

    $yw = date('W')-1 > 0 ? date('Y') . date('W')-1 : date('Y')-1 . '52';

    Whenever that is present, no post are returned, the moment i remove it, posts are returned (but without the date functions ofcourse).

    Can you please double check that code? And did i put it in the right location? Line 3 of my code before the query.

    I also have to end your code 'meta_key' => '_count-views_week-' . $yw; with a comma, and not a ; because the next line has more code.

    I already feel guilty for taking up your day, but i have no more hair left.

  15. Julio Potier
    Member
    Plugin Author

    Posted 2 years ago #

    the value of $yw is 201236 and not 201237 (we are 37, last was 36 so)
    Do you have some values for 201236 ?
    Or are you using the new hack to use/overwrite the "week" ?
    If yes, you do not need the $yw line anymoreand use "_count-views_week" instead.
    You're right, "," and not ";".

    ps : i received a 10€ donation today, was it you ?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.