我想运行一个SQL查询来查找帖子中的所有特色图片,并删除文件本身和对它的任何引用。
有人能告诉我如何做到这一点吗?非常感谢!
我想运行一个SQL查询来查找帖子中的所有特色图片,并删除文件本身和对它的任何引用。
有人能告诉我如何做到这一点吗?非常感谢!
MYSQL查询无法删除文件本身。您必须使用wp\\u delete\\u attachment()。以下是一个概念证明,您可以根据自己的意愿进行更改。特色图像在Posteta中存储为_thumbnail_id
. wp_delete_attachment()
其余的都是为你做的。
<?php
/*
Plugin Name: Delete All Featured Images
Description: Delete all featured images by visiting /?delete-featured-images=1
Version: 0.1
Author: Brian Fegter
Author URI: http://coderrr.com
License: GPL3v2
*/
# USAGE: visit http://yourdomain.com/?delete-featured-images=1
add_action(\'init\', \'foo_bar_delete_featured\', 0);
function foo_bar_delete_featured(){
# Check for logged in state
if(!is_user_logged_in())
return;
# Check for admin role
if(!current_user_can(\'manage_options\'))
return;
# Check for query string
if(isset($_GET[\'delete-featured-images\']) && $_GET[\'delete-featured-images\'] == 1){
global $wpdb;
# Run a DQL to get all featured image rows
$attachments = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = \'_thumbnail_id\'");
foreach($attachments as $attachment){
# Run a DML to remove this featured image row
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = \'$attachment->meta_id\' LIMIT 1");
# Delete attachment DB rows and files
wp_delete_attachment($attachment->meta_value, true);
# Print to screen
show_message(\'Attachment #$attachment->meta_value deleted.\');
}
exit;
}
}
使用此查询可以从数据库中删除缩略图图像和关联的元数据
global $wpdb;
$attachments = $wpdb->get_results( "
SELECT *
FROM $wpdb->postmeta
WHERE meta_key = \'_thumbnail_id\'
" );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->meta_value, true );
}
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = \'_thumbnail_id\'
" );
正在尝试在插件查询中设置“已删除”日期时间,但我不确定如何将更新集与$wpdb一起使用->;准备我的问题是:$cur_date = date(\'Y-m-d G:i:s\'); $rows_affected = $wpdb->query( $wpdb->prepare(" UPDATE $table SET ( removed, post_id, user_id, status )