• Resolved vicetias

    (@vicetias)


    Hi,

    I just installed Jetpack because stats work on AMP posts… so this plugin also works on AMP?

    And with this plugin, can I use a meta key to sort popular posts by day, week, month, year or only all time?

    My template has a code like this:

    <?php global $post; $recent = new WP_Query(array( 'posts_per_page' => 5, 'order' => 'DESC', 'orderby' => 'meta_value_num', 'meta_key' => '?' )); while($recent->have_posts()) : $recent->the_post(); ?>

    • This topic was modified 4 years, 5 months ago by vicetias.
    • This topic was modified 4 years, 5 months ago by vicetias.
    • This topic was modified 4 years, 5 months ago by vicetias.
    • This topic was modified 4 years, 5 months ago by vicetias.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    so this plugin also works on AMP?

    Yes. This plugin only adds text to your site, no JavaScript or anything that wouldn’t be compatible with the AMP plugin.

    with this plugin, can I use a meta key to sort popular posts by day, week, month, year or only all time?

    You can’t really rely on my plugin to do that, no. Instead, I would encourage you to build your own plugin that would query the WordPress.com REST API for Stats data, just like my plugin does. You can find more info about it here:
    https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/

    You may find this specific endpoint to be useful for your needs:
    https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/top-posts/

    Since you’d be using that API on a site where the Jetpack plugin is installed, you have access to utility functions like this one to easily query endpoints that you may be interested in:
    https://github.com/Automattic/jetpack/blob/3010b47431d784ba4771188ca1da2637bb922d97/modules/stats.php#L1615-L1624

    I hope this helps.

    Thread Starter vicetias

    (@vicetias)

    I’m very bad at coding but I’ll look into this. Anyway, I think I could edit or add some lines to your plugin instead of doing my own, because I just need to collect some time periods with new meta keys.

    The only meta key your plugin creates is _post_views, right?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    The only meta key your plugin creates is _post_views, right?

    Yes, that’s correct, but it does not create the key for all your posts when you activate the plugin, it only does it when one visits said post. As such, I am not sure it’s something that would be very useful for you.

    If you think you could make it work and would want a better look at the code, the plugin is also available on GitHub, here:
    https://github.com/jeherve/jp-post-views

    Thread Starter vicetias

    (@vicetias)

    Yes, that’s correct, but it does not create the key for all your posts when you activate the plugin, it only does it when one visits said post. As such, I am not sure it’s something that would be very useful for you.

    It’s useful and I know how it works. Meta keys are created/updated every time posts are viewed.

    I’m currently using that method with BEA post views and before I did with WordPress Popular Posts, but those plugins don’t work on AMP.

    And well, don’t you think you can add those time periods in a future update of your plugin?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    don’t you think you can add those time periods in a future update of your plugin?

    I can certainly see about storing more information in the post meta. I took note of it here:
    https://github.com/jeherve/jp-post-views/issues/9

    I would need to find a good way of using that information in the shortcode though, so as to avoid storing data that the plugin doesn’t use.

    Thanks for the feedback.

    Thread Starter vicetias

    (@vicetias)

    Ok, thank you. If you find a good way let me know, please, or at least some lines to add to my theme functions.

    Something like this:

    update_post_meta( $postID, 'post_views_year',  get_the_post_views_counter(  'year', $post_ID ) );
    update_post_meta( $postID, 'post_views_month', get_the_post_views_counter( 'month', $post_ID ) );
    update_post_meta( $postID, 'post_views_week',  get_the_post_views_counter(  'week', $post_ID ) );
    update_post_meta( $postID, 'post_views_day',   get_the_post_views_counter(   'day', $post_ID ) );
    • This reply was modified 4 years, 5 months ago by vicetias.
    • This reply was modified 4 years, 5 months ago by vicetias.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort popular posts by meta key’ is closed to new replies.