$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'); ?>theme development - Attach media to multiple posts|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)

theme development - Attach media to multiple posts

matteradmin9PV0评论

I have a simple code where I am attaching existing in the library image to a post:

function attachToPost($attachmentId, $postId){
    return wp_update_post(array(
        'ID' => (int)$attachmentId,
        'post_parent' => (int)$postId
    ));
}

The problem is that I can't attach it to a second post. I read in the internet that this is the way that WordPress works, but is there some kind of solution ?

I don't prefer plugins.

I have a simple code where I am attaching existing in the library image to a post:

function attachToPost($attachmentId, $postId){
    return wp_update_post(array(
        'ID' => (int)$attachmentId,
        'post_parent' => (int)$postId
    ));
}

The problem is that I can't attach it to a second post. I read in the internet that this is the way that WordPress works, but is there some kind of solution ?

I don't prefer plugins.

Share Improve this question asked Jan 20, 2019 at 18:10 gdfgdfggdfgdfg 1721 silver badge15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

It is not possible to assign multiple parents to the attachment. However, there is an alternative solution. You can use update_post_meta. to store the attachment id to the post and similarly, you can use function get_post_meta. for getting the attachment id.

What I did was to use set_post_thumbnail( $postId, $attachmentId );. With it, I can have one image attached to many posts.

Post a comment

comment list (0)

  1. No comments so far