• Hi, I need additional checkbox in panel for adding media in my custom edit/add custom post type admin panel. I try to do so by using attachment_fields_to_edit filter, but I can’t figure out how to acces this custom post object from function called by this filter. Can someone help?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The current WP_Post object is passed as the second parameter. When you add your callback, specify 2 as the fourth arg.
    add_filter('attachment_fields_to_edit', 'my_callback', 10, 2);

    Collect the two parameters in your function declaration.

    function my_callback( $fields, $post ) {
      // do something with $fields based on $post value
      return $fields;
    }
    Thread Starter zaqwer

    (@zaqwer)

    In

    function my_callback( $fields, $post ) {
      // do something with $fields based on $post value
      return $fields;
    }

    $post is attachment post_type, I need to acces my custom post_type where I want to modify window for adding media.

    • This reply was modified 4 years, 6 months ago by zaqwer.
    Moderator bcworkz

    (@bcworkz)

    If the attachment belongs to your CPT, the CPT ID will be in $post->parent. You can then use get_post() to get your post object.

    If not, maybe the ID can be determined from something else like $_GET['post'] or maybe in $GLOBALS somewhere.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘attachment_fields_to_edit filter’ is closed to new replies.