WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [Plugin: Post Views Count] Help migrating from WP-Postviews to your plugin

[Resolved] [Plugin: Post Views Count] Help migrating from WP-Postviews to your plugin

  • kiets
    Participant

    @oomskaap

    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/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Julio Potier
    Participant

    @juliobox

    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 😉

    kiets
    Participant

    @oomskaap

    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?

    kiets
    Participant

    @oomskaap

    …. 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?

    Plugin Author Julio Potier
    Participant

    @juliobox

    Do you have posts viewed last week ?

    kiets
    Participant

    @oomskaap

    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-?

    kiets
    Participant

    @oomskaap

    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.

    Plugin Author Julio Potier
    Participant

    @juliobox

    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

    Plugin Author Julio Potier
    Participant

    @juliobox

    Version 2.19.2 updated.
    Try it, thx !

    kiets
    Participant

    @oomskaap

    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.

    Plugin Author Julio Potier
    Participant

    @juliobox

    “_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 ?

    kiets
    Participant

    @oomskaap

    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.

    Plugin Author Julio Potier
    Participant

    @juliobox

    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 … 😉

    kiets
    Participant

    @oomskaap

    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.

    Plugin Author Julio Potier
    Participant

    @juliobox

    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 ?

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] [Plugin: Post Views Count] Help migrating from WP-Postviews to your plugin’ is closed to new replies.