我正在尝试制作一个函数,从数据库中删除所有视频文件+附件帖子和附件帖子。当用户角色切换到“基本”时,应运行此操作。如果post\\u mime\\u类型以“video”开头,因为它可以是video/mp4或video/quicktime等。。。我已经走了这么远,但我无法完全理解:
add_action( \'set_user_role\', function( $user_id, $role, $old_roles )
{
if ( \'basic\' == $role ) {
$args = array(
\'fields\' => \'ids\',
\'posts_per_page\' => -1,
\'post_type\' => \'attachment\',
\'post_mime_type\' => array(\'video/x-ms-asf\',\'video/x-ms-wmv\',\'video/x-ms-wmx\',\'video/x-ms-wm\',\'video/avi\',\'video/divx\',\'video/x-flv\',\'video/quicktime\',\'video/mpeg\',\'video/mp4\',\'video/ogg\',\'video/webm\',\'video/x-matroska\')
);
$all_ids = new WP_Query( $args );
if ( $all_ids->have_posts() ) {
foreach ( $all_ids->posts as $post ) {
wp_delete_attachment( $post->ID, true );
}
}
}
}, 10, 3 );
这不管用,因为我真的不知道我错过了什么!
使用我现有的功能作为指导,效果很好。这将meta\\u值更改为空。
add_action( \'set_user_role\', function( $user_id, $role, $old_roles )
{
if ( \'basic\' == $role ) {
$args = array( \'fields\' => \'ids\',
\'posts_per_page\' => -1,
\'post_type\' => \'slide\',
\'meta_key\' => \'background-video\'
);
$all_ids = new WP_Query( $args );
if ( $all_ids->have_posts() ) {
while ( $all_ids->have_posts() ) {
$all_ids->the_post();
update_post_meta( get_the_ID(), \'background-video\', \'\' );
}
}
wp_reset_postdata();
}
}, 10, 3 );