基于的源代码wp_publish_post()
, 您可以在不接触元数据的情况下更新post状态,同时也不会丢失post转换操作(代码中缺少这些操作),如下所示:
global $wpdb;
// Unkown $get_directories, you have not set it
// in your sample code, I assume it is correct
$directories = get_posts( $get_directories );
foreach( $directories as $directory ){
// Skip already draft posts
if( $directory->post_status != \'draft\' ) {
$wpdb->update( $wpdb->posts, array( \'post_status\' => \'draft\' ), array( \'ID\' => $directory->ID ) );
clean_post_cache( $directory->ID );
$old_status = $directory->post_status;
$directory->post_status = \'draft\';
// Perform transition actions
wp_transition_post_status( \'draft\', $old_status, $directory );
}
}
您还可以包括更新帖子时触发的其他actinos:
do_action( \'edit_post\', $directory->ID, $post );
do_action( "save_post_{$directory->post_type}", $directory->ID, $directory, true );
do_action( \'save_post\', $directory->ID, $directory, true );
do_action( \'wp_insert_post\', $directory->ID, $directory, true );
但是,正如@Rast所说,您的问题很可能是这些操作中的一些被第三方代码(来自另一个插件或主题)钩住了。这些操作由
wp_update_post()
它通常用于更新post元字段,特别是
save_post
行动很可能,当这些操作在没有元数据信息的情况下触发时,就像您的代码一样,第三方代码会删除元数据字段。