有一种更优雅的方法可以使用全局$wpdb
.
function rsc_unpublish_all_ads( $post_id, $post, $update ) {
// select all ads other than the current one
$args = array(
\'nopaging\' => true,
\'post__not_in\' => array($post_id),
\'post_type\' => \'rsc-ads\',
\'post_status\' => \'publish\',
\'fields\'=>\'ids\'
);
$query = new WP_Query($args);
$published_ads = $query->posts;//array of post ids
if ( !empty($published_ads) ) {
global $wpdb;
$ids = implode(\',\', $published_ads);
$wpdb->query("UPDATE $wpdb->posts SET post_status = \'draft\' WHERE ID IN ($ids)");
}
}
add_action( \'save_post_rsc-ads\', \'rsc_unpublish_all_ads\', 10, 3 );
我想你打电话有问题
wp_update_post()
函数仍在运行时。在此实现中,如果
$query
如果正确返回帖子ID,则只需访问数据库一次,即可修复所有帖子,而无需运行循环。注意,我将你的钩子改为特定于“rsc广告”帖子类型,因此它只会在这种帖子类型上触发。