WordPress.org

Ready to get started?Download WordPress

Forums

Jetpack Post Views
[resolved] Don't display 'jetpack-post-views' for scheduled post (3 posts)

  1. sima313
    Member
    Posted 5 months ago #

    Hi Steven!

    I have the following problem on my website.
    I have a miltiauthor website and we recieve a lot of posts from our authors. Our editor uses the sheduled post function becouse it saves time for us.

    Unfortunately when we started using sheduled post function I found out that published posts do not disply unlimited jetpack statistic. I use the following function:

    get_post_meta( $post->ID, 'jetpack-post-views', true );

    Although when I publush posts manually everything is fine.

    I try to solve this problem by manually updating already published post. Only then the post count shows up.

    I looked into database and saw that in the postmeta table after scheduled publishing of the post the 'jetpack-post-views' item is not created. Althought there are 'jetpack-post-views-(daily,monthly,yearly)' items.

    Also when I preview any post which have not been published yet, I do not see post count.

    I think this happens because the function that creates item in the postmeta database table is triggered only when the post in published manualy.

    Could you please investigate this issue and advice how I can modify the code to trigger database updating function for scheduled posts published.

    https://wordpress.org/plugins/jetpack-post-views/

  2. Steven Lambert
    Member
    Plugin Author

    Posted 5 months ago #

    In my code I have a hook set on when the publish post button is activated that just adds the option to the post id and sets it to 0. I'm not sure how schedule posts operate, but I assume they have a similar hook that you could set the same thing for.

    Here is the code for your reference:


    add_action( 'publish_post', array( &$this, 'add_jetpack_meta' ) );
    /* ADD JETPACK POST META ON POST PUBLISH */
    function add_jetpack_meta() {
    global $post;
    add_post_meta( $post->ID, 'jetpack-post-views', 0, true );
    }

  3. sima313
    Member
    Posted 5 months ago #

    I found that for scheduled posts there is 'publish_future_post' add_action parameter, which triggers when the post is published.
    Unfortunately when I first time added...

    add_action( 'publish_future_post', array( &$this, 'add_jetpack_meta' ) );

    ...it does not work.

    I found info about how wp cron operates with post ID here https://core.trac.wordpress.org/ticket/23700#comment:5

    I used instead of "global $post;" -> "$post_id" and it worked!

    Just to conclude. Finally I added the following code:

    // Future Post publish hooks
    add_action( 'publish_future_post', array( &$this, 'add_jetpack_meta_future' ) );

    /* ADD JETPACK POST META ON FUTURE POST PUBLISH */
    function add_jetpack_meta_future( $post_id ) {
    add_post_meta( $post_id, 'jetpack-post-views', 0, true );
    }

    Thank you Steven for your assistance! This topic is resolved now.

Reply

You must log in to post.

About this Plugin

About this Topic