嗨,我想知道当您上传文件并插入帖子时,是否有办法删除上传文件与帖子的关系而不删除文件。例如 pdf 文件。您将在帖子中插入一个链接。我想要做的是,如果我从帖子中删除此链接。从数据库中的帖子中删除此文件的引用。
我面临的问题是我正在使用一个函数来返回所有上传到自定义类型帖子中的 pdf 文件。
function getPdfList(){
global $post, $posts;
$list = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$ext = pathinfo($attachment->guid, PATHINFO_EXTENSION);
if("pdf" == strtolower($ext)){
$list[] = $attachment;
}
}
}
return $list;
}
那么我在我的 php 文件中执行此操作
$args = array( 'post_type' => 'fuerzabasica', 'posts_per_page' => 40 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$pdfs = getPdfList();
echo '<div class="entry-content" style="">';
//the_content();
foreach ($pdfs as $pdf) {
echo $pdf->guid."<br />";
}
echo '</div>';
endwhile;
?>
The problem is that I'm still getting files wich I remove the link from the post, so if my USER upload new files he will get the old files(which the link was removed) and the new one. Is a way to delete the reference of this files of my post??