0

how can use this code for update just post_meta ?

here my code :

function update_all_posts(){

    $args = array('post_type'=> 'post',
         'post_status' => 'publish',
         'posts_per_page'=>50
    );

    $published_posts = get_posts($args);

    foreach($published_posts as $post_update){

       $query = array(
        'ID' => $post_update->ID,
        'post_status' => 'publish',
       );

       wp_update_post( $query, true );  
    }

}

i want use this code :

$value = 'HELLO';
update_post_meta($post_id,'test', $value);
  • 1
    Does the value correspond to a custom field in the post meta table/on on the post type? If so, you'll need that field's machine name/slug to target the data for updating in a query. Also, are all posts really to be assigned the static value of 1000, or assigned something dynamic each time the function is called? – Anson W Han Jan 28 '18 at 4:51
  • tnx for replay. i want use just update_post_meta() in the query : wp_update_post(); in the all posts in my site – Pedroxam Jan 29 '18 at 14:48
  • 1
    It's unclear WHERE you want to use update_post_meta. As it stands, if you have $post_id with the ID of a post, the code you posted works just fine. However, whether it solves whatever task you are trying to solve is another issue and really depends on the task (and you'll have to explain more about that task). – janh Jan 29 '18 at 15:21

Your Answer

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

Browse other questions tagged or ask your own question.