我正在寻找一种方法,根据自定义字段值更改某些帖子的帖子状态,例如从已发布改为挂起。这怎么可能呢?
我的解决方案:
$post_ids = array( 1235, 1234, 1228, 1221, 1211, 1212, 1208, 1200 );
foreach($post_ids as $post_id) {
$post = array( \'ID\' => $post_id, \'post_status\' => \'pending\' );
wp_update_post($post);
}
最合适的回答,由SO网友:jetlej 整理而成
将其粘贴到任何页面并加载该页面(或将其作为functions.php中的函数并在某处调用该函数)
$args = array(
\'nopaging\' => true, // Loop through all posts at once
\'meta_key\' => \'YOUR_CUSTOM_FIELD_NAME\', // Replace with the name of your custom field
\'meta_value\' => \'YOUR_CUSTOM_FIELD_VALUE\', // The value of that field you\'re matching for
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Change the status of each post to pending
$updated = wp_update_post( array(\'ID\' => $the_query->post->ID, \'post_status\' => \'pending\' ));
// Check to see if loop is returning posts, and if they were updated
echo \'Post #\' . $the_query->post->ID . \' - \' . $updated;
}
}