WordPress.org

Ready to get started?Download WordPress

Forums

On publish post update post meta value (1 post)

  1. BPweba
    Member
    Posted 1 year ago #

    I'd like to ask for a little help why code below doesnt work to upadate post meta value and display it as float. It did work at start but displayed 7 insted 7.4

    function addAvg() {
    global $post;
    	while (have_posts()) : the_post();
    
        $xxx = get_post_meta($post->ID, 'x', true);
        $xxx1 = get_post_meta($post->ID, 'x1', true);
        $xxx2 = get_post_meta($post->ID, 'x2', true);
        $xxx3 = get_post_meta($post->ID, 'x3', true);
        $xxx4 = get_post_meta($post->ID, 'x4', true);
    
        $avgcho = ($xxx + $xxx1 + $xxx2 + $xxx3 + $xxx4)/5;
    
        update_post_meta($post->ID, 'avg', $avgcho);
    	endwhile;
    }
    add_action('publish_post', 'addAvg');

    Thanks for any help :)

Topic Closed

This topic has been closed to new replies.

About this Topic