$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>images - Dupplication of Custom Post Type while adding attachment|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

images - Dupplication of Custom Post Type while adding attachment

matteradmin7PV0评论

I'm facing an annoying issue that I can't solve.

I've made this code in order to add or update an attachment on a custom post type :

function add_update_attachment( $postID, $photo ) {

  if ($photo == '') return;
  $basename = basename($photo);
  $basenameExp = explode("?", $basename);
  $name = $basenameExp[0];
  $attach = array_shift(get_attached_media('image', $postID));
    $attachName = basename( $attach->guid );
  $wp_upload_dir = wp_upload_dir();
  $fileName = $wp_upload_dir['path']. "/".urldecode($name);

    $fileType = wp_check_filetype( $name, null );
  // echo '<pre>'.print_r($wp_upload_dir, true).'</pre>';
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' .  $name ,
        'post_mime_type' => $fileType['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', $name ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    if ( str_replace('%20', ' ', $attachName) !== $photo ) {
        wp_update_post( array( 'ID' => $attach->ID, 'post_parent' => 0) );
        copy ( $photo, $wp_upload_dir['path']. "/" . urldecode($name) );
        $attachID = wp_insert_attachment( $attachment, $fileName, $postID );
        $attachData = wp_generate_attachment_metadata( $attachID, $fileName );
        wp_update_attachment_metadata( $attachID, $attachData );
        set_post_thumbnail( $postID, $attachID );
    }
}

The problem is that : some of my post are dupplicating for no reason.

I have this post (I'm showing a custom field only) :

Array
(
    [0] => LieuTemp001
    [1] => LieuTemp002
)

And the last one is dupplicated. Why ? I have no problem if I don't use this function (but I need to use it, obsviously)

I'm facing an annoying issue that I can't solve.

I've made this code in order to add or update an attachment on a custom post type :

function add_update_attachment( $postID, $photo ) {

  if ($photo == '') return;
  $basename = basename($photo);
  $basenameExp = explode("?", $basename);
  $name = $basenameExp[0];
  $attach = array_shift(get_attached_media('image', $postID));
    $attachName = basename( $attach->guid );
  $wp_upload_dir = wp_upload_dir();
  $fileName = $wp_upload_dir['path']. "/".urldecode($name);

    $fileType = wp_check_filetype( $name, null );
  // echo '<pre>'.print_r($wp_upload_dir, true).'</pre>';
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' .  $name ,
        'post_mime_type' => $fileType['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', $name ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    if ( str_replace('%20', ' ', $attachName) !== $photo ) {
        wp_update_post( array( 'ID' => $attach->ID, 'post_parent' => 0) );
        copy ( $photo, $wp_upload_dir['path']. "/" . urldecode($name) );
        $attachID = wp_insert_attachment( $attachment, $fileName, $postID );
        $attachData = wp_generate_attachment_metadata( $attachID, $fileName );
        wp_update_attachment_metadata( $attachID, $attachData );
        set_post_thumbnail( $postID, $attachID );
    }
}

The problem is that : some of my post are dupplicating for no reason.

I have this post (I'm showing a custom field only) :

Array
(
    [0] => LieuTemp001
    [1] => LieuTemp002
)

And the last one is dupplicated. Why ? I have no problem if I don't use this function (but I need to use it, obsviously)

Share Improve this question asked Nov 21, 2018 at 15:54 MorganMorgan 13710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Got it :

wp_update_post( array( 'ID' => $attach->ID, 'post_parent' => 0) );

to

wp_update_post( array( 'ID' => $postID 'post_parent' => 0) );
Post a comment

comment list (0)

  1. No comments so far