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 Answer
Reset to default 0Here 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
版权声明:本文标题:query - Bulk Post update_post_meta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736725431a1949701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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