• Hello everybody,

    I am using the loop below in my category template. What I want is the WordPress to update a post meta named “hits” whenever a user clicks on the link. But the problem is that WordPress updates the post meta “hits” of all the posts available on the page. What is the problem?

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    		<?php $gif = get_post_meta($post->ID, 'gif', true); ?>
    		<?php $view = get_post_meta($post->ID, 'hits', true); ?>
    		<div class="item">
    			<a href="<?php if ($gif!="") { echo $gif; } else { echo $url; } ?>" title="View photo in full size" rel="prettyPhoto" onclick="<?php the_id(); ?><?php $hits = $view + 1; update_post_meta( $post->ID , 'hits', $hits ); ?>" ><img src="<?php echo $url; ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" /></a>
    			<div class="boxes">
    				<div class="like"><?php if(function_exists(getILikeThis)) getILikeThis('get'); ?></div>
    				<div class="views"><?php if ($view!="") { echo $view; } else { echo "0"; } ?></div>
    				<div class="share">
    				<a href="https://www.facebook.com/share.php?v=4&src=bm&u=<?php the_permalink(); ?>" title="Share on FB"><img src="https://picogif.ir/wp-content/uploads/2014/08/facebook.png" alt="Facebook" /></a>
    				<a href="https://www.cloob.com/share/link/add?url=<?php the_permalink(); ?>&title=<?php echo get_the_title(); ?>" title="Share on Cloob"><img src="https://picogif.ir/wp-content/uploads/2014/08/cloob.png" alt="Cloob" /></a>
    				</div>
    			</div>
    		</div>
    		<?php endwhile; else: ?>
    		<div class="error-404">
    			No posts was found.
    		</div>
    		<?php endif; ?>

    Please help me fast. I’m a bit in hurry.
    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I guess what you are trying to say is that the hits meta should get updated for a single post! In this case, you need to add the update_post_meta inside the single.php of your theme.

    Currently you are calling update_post_meta inside the loop and therefore it is getting updated for all the posts in the loop.

    Your single.php should have the following as shown below –

    global $post;
    $view = get_post_meta($post->ID, 'hits', true);
    $hits = $view + 1;
    update_post_meta( $post->ID , 'hits', $hits );

    This will do the job.

    But I want it to work in category archive pages! Please help me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post media updating problem’ is closed to new replies.