質問

Hi I´m trying to know if there are someway to remove the relation of the uploaded files with a post WITHOUT deleted the files, when you upload a file and inserted into the post. For example a pdf file. you will get a link inserted into the post. what I want to do is if I remove this link from the post. Delete the reference of this file from the post in the database.

The problem I'm facing is that I'm using a function wich return all the pdf files uploaded into a custom type of post.

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;

}

So then I'm doing this in my php file

        $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??

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top