bulk_edit_posts

Process the post data for the bulk editing of posts.

Description

(array) bulk_edit_posts( (null) $post_data = null ); 

Updates all bulk edited posts/pages, adding (but not removing) tags and categories. Skips pages when they would be their own parent or child.

Returns (array)

Parameters (1)

0. $post_data — Optional. (null) => null
Optional, the array of post data to process if not provided will use $_POST superglobal.

Usage

  1. if ( !function_exists( 'bulk_edit_posts' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Optional, the array of post data to process if not provided will use $_POST superglobal. 
  5. $post_data = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bulk_edit_posts($post_data); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function bulk_edit_posts( $post_data = null ) { 
  2. global $wpdb; 
  3.  
  4. if ( empty($post_data) ) 
  5. $post_data = &$_POST; 
  6.  
  7. if ( isset($post_data['post_type']) ) 
  8. $ptype = get_post_type_object($post_data['post_type']); 
  9. else 
  10. $ptype = get_post_type_object('post'); 
  11.  
  12. if ( !current_user_can( $ptype->cap->edit_posts ) ) { 
  13. if ( 'page' == $ptype->name ) 
  14. wp_die( __('Sorry, you are not allowed to edit pages.')); 
  15. else 
  16. wp_die( __('Sorry, you are not allowed to edit posts.')); 
  17.  
  18. if ( -1 == $post_data['_status'] ) { 
  19. $post_data['post_status'] = null; 
  20. unset($post_data['post_status']); 
  21. } else { 
  22. $post_data['post_status'] = $post_data['_status']; 
  23. unset($post_data['_status']); 
  24.  
  25. if ( ! empty( $post_data['post_status'] ) ) { 
  26. $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 
  27.  
  28. if ( 'inherit' == $post_data['post_status'] ) { 
  29. unset( $post_data['post_status'] ); 
  30.  
  31. $post_IDs = array_map( 'intval', (array) $post_data['post'] ); 
  32.  
  33. $reset = array( 
  34. 'post_author', 'post_status', 'post_password',  
  35. 'post_parent', 'page_template', 'comment_status',  
  36. 'ping_status', 'keep_private', 'tax_input',  
  37. 'post_category', 'sticky', 'post_format',  
  38. ); 
  39.  
  40. foreach ( $reset as $field ) { 
  41. if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) ) 
  42. unset($post_data[$field]); 
  43.  
  44. if ( isset($post_data['post_category']) ) { 
  45. if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) ) 
  46. $new_cats = array_map( 'absint', $post_data['post_category'] ); 
  47. else 
  48. unset($post_data['post_category']); 
  49.  
  50. $tax_input = array(); 
  51. if ( isset($post_data['tax_input'])) { 
  52. foreach ( $post_data['tax_input'] as $tax_name => $terms ) { 
  53. if ( empty($terms) ) 
  54. continue; 
  55. if ( is_taxonomy_hierarchical( $tax_name ) ) { 
  56. $tax_input[ $tax_name ] = array_map( 'absint', $terms ); 
  57. } else { 
  58. $comma = _x( ', ', 'tag delimiter' ); 
  59. if ( ', ' !== $comma ) 
  60. $terms = str_replace( $comma, ', ', $terms ); 
  61. $tax_input[ $tax_name ] = explode( ', ', trim( $terms, " \n\t\r\0\x0B, " ) ); 
  62.  
  63. if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) { 
  64. $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'"); 
  65. $children = array(); 
  66.  
  67. for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { 
  68. $children[] = $parent; 
  69.  
  70. foreach ( $pages as $page ) { 
  71. if ( $page->ID == $parent ) { 
  72. $parent = $page->post_parent; 
  73. break; 
  74.  
  75. $updated = $skipped = $locked = array(); 
  76. $shared_post_data = $post_data; 
  77.  
  78. foreach ( $post_IDs as $post_ID ) { 
  79. // Start with fresh post data with each iteration. 
  80. $post_data = $shared_post_data; 
  81.  
  82. $post_type_object = get_post_type_object( get_post_type( $post_ID ) ); 
  83.  
  84. if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) { 
  85. $skipped[] = $post_ID; 
  86. continue; 
  87.  
  88. if ( wp_check_post_lock( $post_ID ) ) { 
  89. $locked[] = $post_ID; 
  90. continue; 
  91.  
  92. $post = get_post( $post_ID ); 
  93. $tax_names = get_object_taxonomies( $post ); 
  94. foreach ( $tax_names as $tax_name ) { 
  95. $taxonomy_obj = get_taxonomy($tax_name); 
  96. if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) 
  97. $new_terms = $tax_input[$tax_name]; 
  98. else 
  99. $new_terms = array(); 
  100.  
  101. if ( $taxonomy_obj->hierarchical ) 
  102. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') ); 
  103. else 
  104. $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') ); 
  105.  
  106. $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms ); 
  107.  
  108. if ( isset($new_cats) && in_array( 'category', $tax_names ) ) { 
  109. $cats = (array) wp_get_post_categories($post_ID); 
  110. $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); 
  111. unset( $post_data['tax_input']['category'] ); 
  112.  
  113. $post_data['post_type'] = $post->post_type; 
  114. $post_data['post_mime_type'] = $post->post_mime_type; 
  115. $post_data['guid'] = $post->guid; 
  116.  
  117. foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) { 
  118. if ( ! isset( $post_data[ $field ] ) ) { 
  119. $post_data[ $field ] = $post->$field; 
  120.  
  121. $post_data['ID'] = $post_ID; 
  122. $post_data['post_ID'] = $post_ID; 
  123.  
  124. $post_data = _wp_translate_postdata( true, $post_data ); 
  125. if ( is_wp_error( $post_data ) ) { 
  126. $skipped[] = $post_ID; 
  127. continue; 
  128.  
  129. $updated[] = wp_update_post( $post_data ); 
  130.  
  131. if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { 
  132. if ( 'sticky' == $post_data['sticky'] ) 
  133. stick_post( $post_ID ); 
  134. else 
  135. unstick_post( $post_ID ); 
  136.  
  137. if ( isset( $post_data['post_format'] ) ) 
  138. set_post_format( $post_ID, $post_data['post_format'] ); 
  139.  
  140. return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );