对于垃圾箱中的所有帖子,我需要执行以下操作:
1) 将其状态从“垃圾”更改为“发布”
2) 将名为“new_status”的自定义字段添加到那些值为“已删除”的帖子。
理想情况下,我需要使用一个可以放置在functions.php中的函数。
我想我需要从这样的事情开始......
$myconversion = $wpdb->get_row("SELECT * FROM 'wp_posts' WHERE post_status = 'publish'");
...但是MYSQL不是我的强项。
任何帮助都会很棒。
最佳答案
幸运的是,你不必了解 SQL,你有强大的 WP_Query 函数,所以我认为你需要这样的东西(将其添加到你的functions.php文件中)
function wp_update_all_posts_status($params = null){
$args = array(
'nopaging' => true, // Loop through all posts at once
'post_status' => array('trash'), //Get posts "from the trash"
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Change the status the post to publish
$updated = wp_update_post( array('ID' => $the_query->post->ID, 'post_status' => 'publish' ));
//add the post "new_status" meta
add_post_meta( $the_query->post->ID, 'new_status', 'deleted' );
}
}
}
wp_update_all_posts_status();
关于mysql - 从垃圾箱中删除帖子,为其添加自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36573954/