图像url作为附件post\\u类型存储在wp\\u posts中,其相应的post作为父对象。来自Codex,请参阅Template Tags Show All Attachments
我相信这样的东西可以在循环外检索DB中的所有附件:
global $post;
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'post_parent\' => null
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_title();
the_attachment_link( $post->ID, false );
the_excerpt();
}
wp_reset_postdata();
也许像这样的东西可以找到并清除/uploads/directory中不再可用的内容:
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1
);
$attachments = get_posts( $args );
foreach( $attachments as $attch ){
$file = get_attached_file( $attch->ID );
if( !file_exists( $file ) ){
wp_delete_post( $attch->ID, false );
}
}
Reference for get_attached_file()
Reference for wp_delete_post()
仔细研究这些问题,就可以很容易地找到实现所需清理的方法。