admin管理员组

文章数量:1128469

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);

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);
Share Improve this question edited Jan 29, 2018 at 14:50 Pedroxam asked Jan 28, 2018 at 2:40 PedroxamPedroxam 32 bronze badges 3
  • 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 Commented Jan 28, 2018 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 Commented Jan 29, 2018 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 Commented Jan 29, 2018 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 0

Here is one function that I made for bulk post meta update that you can use.

/*
 * Optimized way to add multiple usermeta keys and values
 * @author Ivijan-Stefan Stipic
 */
if( !function_exists('bulk_update_post_meta') ) : function bulk_update_post_meta(int $post_id, array $meta_data) {
    global $wpdb;

    $transactions = 0;

    foreach ($meta_data as $meta_key => $meta_value) {
        $meta_data[$meta_key] = [
            'exists' => $wpdb->get_var($wpdb->prepare(
                "SELECT COUNT(*) FROM `{$wpdb->postmeta}` WHERE `post_id` = %d AND `meta_key` = %s",
                $post_id,
                $meta_key
            )) > 0,
            'value' => maybe_serialize($meta_value)
        ];
    }

    $wpdb->query('START TRANSACTION');

    foreach ($meta_data as $meta_key => $meta_object) {

        $meta_value = $meta_object['value'];

        if ( $meta_object['exists'] ) {
            if( $wpdb->query($wpdb->prepare(
                "UPDATE `{$wpdb->postmeta}` SET `meta_value` = %s WHERE `post_id` = %d AND `meta_key` = %s",
                $meta_value,
                $post_id,
                $meta_key
            )) !== false ) $transactions++;
        } else {
            if( $wpdb->query($wpdb->prepare(
                "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) VALUES (%d, %s, %s)",
                $post_id,
                $meta_key,
                $meta_value
            )) !== false ) $transactions++;
        }
    }

    $wpdb->query('COMMIT');

    clean_post_cache($post_id);

    return $transactions === count($meta_data);
} endif;

You simply put an array with 'meta_key' => 'Value'

Example:

bulk_update_post_meta( $post_id, [
    'ivi_test__1' => mt_rand( 1, 100 ),
    'ivi_test__2' => mt_rand( 1, 100 ),
    'ivi_test__3' => mt_rand( 1, 100 ),
    'ivi_test__4' => mt_rand( 1, 100 ),
    'ivi_test__5' => mt_rand( 1, 100 ),
    'ivi_test__6' => mt_rand( 1, 100 ),
    'ivi_test__7' => mt_rand( 1, 100 ),
    'ivi_test__8' => mt_rand( 1, 100 ),
    'ivi_test__9' => mt_rand( 1, 100 ),
    'ivi_test__10' => mt_rand( 1, 100 ),
    'ivi_test__11' => mt_rand( 1, 100 ),
    'ivi_test__12' => mt_rand( 1, 100 ),
] );

本文标签: queryBulk Post updatepostmeta