wp query - How can I update post meta inside a WP_Query loop or the WordPress loop?

How can I update post meta inside the WP_Query? Does this mean inside the loop? Sorry, I'm not too technical about

How can I update post meta inside the WP_Query? Does this mean inside the loop? Sorry, I'm not too technical about this.

This is the code I'm using:

   <?php
    if(isset($_POST['jolly'])){
    update_post_meta($post->ID , 'carry', 3); 
    }
    ?>
    <form action='' method='POST'>
    <input type='submit' name='jolly' value='test' />
    </form>

Are there alternative ways?

How can I update post meta inside the WP_Query? Does this mean inside the loop? Sorry, I'm not too technical about this.

This is the code I'm using:

   <?php
    if(isset($_POST['jolly'])){
    update_post_meta($post->ID , 'carry', 3); 
    }
    ?>
    <form action='' method='POST'>
    <input type='submit' name='jolly' value='test' />
    </form>

Are there alternative ways?

Share Improve this question edited Nov 12, 2016 at 20:25 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Nov 12, 2016 at 20:15 user106861user106861
Add a comment  | 

1 Answer 1

Reset to default 1

The Loop is PHP code used by WordPress to display posts. You can see how a loop is constructed on generatewp.

// WP_Query arguments
$args = array ();

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        $post = get_post();
        if ( isset( $_POST[ 'jolly' ] ) ) {
            update_post_meta( $post->ID, 'jolly', $_POST[ 'jolly' ] );
        }
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
?>

<form action='' method='POST'>
    <input type='submit' name='jolly' value='test'/>
</form>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745177086a4615242.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信