0
votes

I need to know the id of the post current not the id of the attachment.

function add_filter_upload_image($attachment_id){
   // code filter 
}
add_filter('add_attachment', 'add_filter_upload_image', 10, 1);

I've tried with all these ways and none works.

  • global $post; echo $post->id;
  • $post_id = $_GET['post'];
  • $post_id = $_POST['post'];
  • $post_id = $_REQUEST['post_id'];

Any ideas please?

2

2 Answers

0
votes

If you upload an attachment inside some post, that post will be the parent of that attachment. So, this will work for your case:

 function add_filter_upload_image($attachment_id){
   $post_id=wp_get_post_parent_id( $attachment_id );
  }
 add_filter('add_attachment', 'add_filter_upload_image', 10, 1);
0
votes

I'm sorry, it's resolved. I forgot to attach the post in the wp_enqueue_media with the rush.

wp_enqueue_media( array( 'post' => $post->ID ) );

Thanks for answering