• Resolved 182tage

    (@182tage)


    Dear Hector,

    Thank you for this great plugin which is indeed a kind of heroic act.

    Because I have some special needs (f.e. filtering footnotes, picture numbers, navigating elements and so on in the excerpt and singular/plural for no. of views) i made a custom version of a “most viewed” page for day, week, month and all.

    For that I used your code to implement accordingly wp custom fields.

    Here are excerpts of my code:

    ...
    $args = array(
         	'meta_key' => $feld,
             'orderby' => 'meta_value_num',
             'order' => 'DESC',
    	 'ignore_sticky_posts' => '1',
             'posts_per_page' => $zahl
    ...
    
    while($last_loop->have_posts() && $counter <= $zahl ) : $last_loop->the_post();
    $ID = $last_loop->post->ID;
    $thumb = getThumb($last_loop->post->ID); 
    $url = get_permalink($last_loop->post->ID );
    $text_title = get_the_title( $last_loop->post->ID );
    $excerpt = clearTeaser(get_the_excerpt($last_loop->post->ID));
    $views = wpp_get_views($last_loop->post->ID, $mode, true);
    
    ...
    
    endwhile; 
    
    

    $feld is f.e. ‘views_monthly’, $zahl is f.e. ’50’, $mode is f.e. ‘monthly’

    Everything seemed to work fine. i also added a shortcode that works. But there are some small issues in detail:

    • For “all” and “month” the sorting is correct. But for “day” and “week” either the sequence is wrong or the number for the views is determined wrong or both. There are even posts with 0 views in the listing (should not), followed by some with two or three views. Perhaps you have an idea what went wrong?https://182tage.net/blog/test-heute
    • Additionally to orderby the custom field f.e. “month” there should be possible a second orderby date of last view, if numbers are equal. Is there a custom field for this already? XXX would be that custom field perhaps:
     ...
    $args = array(
         	'meta_key' => $feld,
             'orderby'        => [
                 'meta_value_num' => 'DESC',
                 'XXX' => 'DESC'],
    	 'ignore_sticky_posts' => '1',
             'posts_per_page' => $zahl
    ...
    • Last not least I would like to use the parameter “year” in addition to “day”, “week” and “month”. Would you add this option? I already gave a donation … some more could follow … thx a lot.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @182tage,

    The problem is most likely that you’re using something like this to create your post meta:

    wpp_get_views($post_ID, 'your_meta_key', true);

    As per the documentation the third parameter, number format, tells the function whether to return a formatted number (eg. 1,234) or a non-formatted number (eg. 1234).

    To be able to sort anything by meta_value_num, the meta value needs to be an actual number without any formatting (eg. 1234) so WP_Query’s sorting works as expected.

    So if you’re storing formatted numbers in your post meta field WP_Query gets confused and doesn’t sort posts correctly.

    So make sure that when storing views count as meta your function call looks like this instead:

    wpp_get_views($post_ID, 'your_meta_key', false);

    Last not least I would like to use the parameter “year” in addition to “day”, “week” and “month”. Would you add this option?

    I’ll think about it, thanks for the suggestion!

    I already gave a donation …

    Donations are not required, I do appreciate the gesture though so thank you!

    • This reply was modified 9 months, 3 weeks ago by Hector Cabrera. Reason: Reworded for clarity
    Plugin Author Hector Cabrera

    (@hcabrera)

    Small correction: with ‘your_meta_key’ I meant ‘your_time_range’ ??

    Thread Starter 182tage

    (@182tage)

    Hi Hector,

    sorry if i wasn’t clear enough.

    The custom field used for sorting comes from the function you posted here (example for daily), which i placed in the functions.php.

    function custom_wpp_update_postviews($postid) {
    
    $accuracy = 100; // 100 for test purpose
    
    if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) {
            update_post_meta(
            $postid,
            'views_daily',
            wpp_get_views($postid, 'daily', false)
        );
    }
    
    add_action('wpp_post_update_views', 'custom_wpp_update_postviews');

    So there is no problem with number format for sorting by WP_Query.

    The additional code line

    wpp_get_views($post_ID, 'your_meta_key', false);

    in the loop simply generates the content for displaying, there is no impact on sorting here. Additionally i tested it though. Either the parameter is true or false in this line or in your function custom_wpp_update_postviews has no impact on sorting. Probably the line

    'meta_value_num' => 'DESC',

    makes the typecast automatically.

    The problem as to see here https://182tage.net/blog/test-heute still exists. For smaller numbers as for daily and weekly the sequence is wrong, also listing items with 0 views. For larger numbers as with monthly it works correct (so far as i can see). For example for daily the sequence is 4,3,1,0,0,1,2,0,0,0. There should not be any item with zero views in that list. The sequence should be 4,3,2,1,1 – finish.

    There was a second question: Sorting should be possible with two orderby’s: First for the time range – and if there is more than one item with the same number of views it should be sorted by date of last view (DESC) additionally. Like this about:

    'orderby'        => [
                 'meta_value_num' => 'DESC',
                 'XXX' => 'DESC'],

    Where XXX could perhaps be the name of a custom field for the dates of last views. Is there any possibility for such a custom field?

    Thank you for your advice!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Honestly no idea then. I don’t see any obvious issues with your code, at least not with the portions you’ve shared so far.

    As people over Stack Overflow would say, try sharing a Minimal, Reproducible Example of your code to see if I can replicate the issue you’re having with your query here.

    Thread Starter 182tage

    (@182tage)

    After some days of watching, comparing and analyzing i found out:

    1. There is a time discrepancy between your function custom_wpp_update_postviews() and wpp_get_views().
    2. wpp_get_views() ist working just in time (that means correct) but custom_wpp_update_postviews ist updated only about every second day.
    3. As a result the sequence (produced by orderby phrase) is old whereas the shown views are fresh. And that looks like a wrong sorting.

    The question is therefore how to refresh the data in the custom fields like views_daily more often, the best would be exactly in the same period as wpp_get_views(), simultaneously.

    As i mentioned twice there is also still an issue with a second orderby, if the number of views in the listing is equal. Using your plugin’s shortcut this sorting is by date/time of last view. I have to emulate this and it would be great to know if there is an accordingly custom field for this.

    Plugin Author Hector Cabrera

    (@hcabrera)

    You could try hooking into wpp_post_update_views to update your custom fields in real-time, note though this won’t likely be great for performance which is why I recommend using the other method instead.

    Thread Starter 182tage

    (@182tage)

    Well thank you. When this way is blocked, i will try the way with

    $wpdb->get_results()

    to use a direct SQL-statement. A simple Query is needed with a subquery for the views per item. This should only need a few ten milliseconds. I think this discussion can be closed.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Orderby not correct with smaller numbers, please ‘year’ param’ is closed to new replies.