WordPress.org

Support

Support » How-To and Troubleshooting » assistance with update_post_meta issue

assistance with update_post_meta issue

Viewing 4 replies - 1 through 4 (of 4 total)
  • Has anyone may be even had anything similar when using update_post_meta, where they are finding it’s updating two records instead of one?

    regards

    Angus

    Justin Tadlock

    @greenshady

    WordPress God

    Just ran into the same issue with similar code. update_post_meta is adding the meta value to the post and the post published after it. I’ll keep looking for solutions.

    Here’s the code for reference:

    add_action( 'template_redirect', 'entry_views_update' );
    
    function entry_views_update() {
    	global $wp_query;
    
    	/* If we're on a singular view of a post, calculate the number of views. */
    	if ( is_singular( apply_filters( 'entry_views_post_types', '' ) ) ) {
    
    		/* Allow devs to override the meta key used. By default, this is 'Views'. */
    		$meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
    
    		/* Get the ID of the current post being viewed. */
    		$post_id = $wp_query->get_queried_object_id();
    
    		/* Get the number of views the post currently has. */
    		$old_views = get_post_meta( $post_id, $meta_key, true );
    
    		/* Add +1 to the number of current views. */
    		$new_views = absint( $old_views ) + 1;
    
    		/* Update the view count with the new view count. */
    		update_post_meta( $post_id, $meta_key, $new_views, $old_views );
    	}
    }
    Justin Tadlock

    @greenshady

    WordPress God

    Tracked down an answer here, which is a little outdated but proved useful:
    http://wordpress.org/support/topic/problem-with-add_filter-while-developing-a-plugin?replies=5

    You’ll have to use this to keep it from getting called twice:

    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );

    And, I created a Trac ticket:
    http://core.trac.wordpress.org/ticket/14568

    The ticket is saying that it’s a new feature of FireFox: Pre-fetching the rel=”next”

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘assistance with update_post_meta issue’ is closed to new replies.
Skip to toolbar