Support » Plugin: Jetpack Post Views » get_post_meta function returning post ID, not view count

  • Resolved rhiever

    (@rhiever)


    I’m trying to use this plugin in conjunction with the Code Snippets plugin so I don’t have to edit the underlying theme’s code. (My theme updates fairly frequently.) Thus I’m trying to put the view # code in the post text itself, not the underlying PHP code.

    The normal

    echo get_post_meta( $post->ID, ‘jetpack-post-views’, true );

    code doesn’t work, I’m assuming because the $post variable doesn’t exist in the post text scope.

    I then tried

    echo get_post_meta( (int)the_ID(), ‘jetpack-post-views’, true );

    but that only outputs the post ID. Running

    echo (int)the_ID();

    prints the post ID fine, so I know it’s getting the post ID okay. What’s going on?

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Steven Lambert

    (@straker503)

    There are a few things you could try. You can try getting the global post variable before getting the post views by using global $post.

    If that doesn’t work, then you can try using get_the_ID(). the_ID() prints the ID of the post and doesn’t return it for use, so that is probably why it isn’t working.

    Aha! Using get_the_ID() did the trick. Funny what a difference 4 characters can make. Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_post_meta function returning post ID, not view count’ is closed to new replies.