0

I've this code for post count, it almost works for me but, when i apply <?php echo tie_views();?> in recent, popular or related posts lists. post counts increase there too. I want to control it and only it works in single posts. Any body can help me?

 function tie_views(){
global $post;       

$count_key  = 'tie_views';
$count      = get_post_meta($postID, $count_key, true);
$count      = @number_format($count);    

if( !defined('WP_CACHE') || !WP_CACHE ){
    $count++;
    update_post_meta($postID, $count_key, (int)$count);
}
elseif( ($count) ){
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, 0 );
    $count = 0;
}


return '<div class="post-view"><span>'.$count.'</span> ' .__( 'Views' , 'tie').'</div>';

}

0

On a few separate notes:

  • I think your elseif check is probably intended to be elseif( empty( $count ) ), as currently it would result in the count being reset to 0 whenever WP_CACHE is set and the post has a 'tie_views' field - though I may be misinterpreting your intentions.
  • One call to update meta-data is faster than two to delete it then add it again.
  • number_format() produces a string often containing non-numerical characters. Incrementing such a string or casting it to an integer will produce all sorts of unexpected behaviors (i.e. (int)'5,000' produces 5). Format the number only after you've performed all relevant mathematical operations.
  • $postID is never defined, nor is it a global variable. Use get_the_ID() to get the ID of the current post within The Loop.
  • The use of @ to suppress errors is typically discouraged, and is often indicative poor practices (warnings and errors should be corrected or handled, not ignored). In this case, we can eliminate it by falling back to a default integer 0 instead of attempting to number_format() null.

All of that said, you can achieve your desired affect by creating a boolean $update argument for your function. This way you can explicitly force your function to update the count (or not to) by calling tie_views( true ) or tie_views( false ).

Going one step further, you can give your function the logic to determine whether or not it should update the count on it's own in the case that the $update argument was not specified.

function tie_views( $update = null ){
  $count_key  = 'tie_views';
  $post_id    = get_the_ID();
  $count      = get_post_meta( $post_id, $count_key, true );

  // If no 'tie_views' meta-data exists for this post, then the count is 0
  if( empty( $count ) )
    $count = 0;

  // If $update was not set, set it to 'true' if in the main loop and displaying one post.
  if( null === $update )
    $update = in_the_loop() && is_singular();      

  // If the count should be updated and advanced caching is disabled,
  //   increment the count and save it's new value.
  if( $update && ( !defined( 'WP_CACHE' ) || !WP_CACHE ) )
    update_post_meta( $post_id, $count_key, ++$count );

  // Return markup with a formatted count-string
  return '<div class="post-view"><span>' . number_format( $count ) . '</span> ' . __( 'Views' , 'tie' ) . '</div>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.