0

嗨,我想知道当您上传文件并插入帖子时,是否有办法删除上传文件与帖子的关系而不删除文件。例如 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??

4

1 回答 1

0

WordPress will maintain a parent/child relationship between posts and uploaded files. To prevent this you will have to make sure the post_parent value of every attachment is 0 instead of the parent posts ID.

You could accomplish this with a save_post filter, in which you'll loop through any attachments returned by get_posts(array('post_type' => 'attachment', 'post_parent' => $post_id, 'posts_per_page' => -1)); setting post_parent to 0. $post_id is the first parameter of your filter function.

于 2012-02-14T20:51:13.593 回答