Do attachments added to custom post types have a post parent?

On attachment pages, I have a link back to the post/page where the file was uploaded. (This is not displayed for files that were directly added to the Media Library and that’s fine.)

For attachments added to posts and pages, the following code generates the link:

$post = get_post( get_the_ID() );

if ( $post->post_parent ) {
  echo '<a href="' . get_permalink( $post->post_parent ) . '">' . get_the_title(
  $post->post_parent ) . '</a>';
}

However, when I try viewing an attachment that was uploaded to a custom post type, it doesn’t work.

If I display a var_dump of $post, I get the following:

object(WP_Post)#398 (24) { ["ID"]=> int(789) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2015-04-17 13:32:39" ["post_date_gmt"]=> string(19) "2015-04-17 13:32:39" ["post_content"]=> string(0) "" ["post_title"]=> string(5) "image" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "inherit" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(5) "image" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2015-04-17 13:32:39" ["post_modified_gmt"]=> string(19) "2015-04-17 13:32:39" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "wp-content/uploads/2015/04/image.jpg" ["menu_order"]=> int(0) ["post_type"]=> string(10) "attachment" ["post_mime_type"]=> string(10) "image/jpeg" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

The concerning part is ["post_parent"]=> int(0) implying that files attached to custom posts don’t have a parent.

Can anyone confirm this? Or could this be an error with that way I have setup the custom post type?

I have tried both hierarchical and non-hierarchical types, and with and without ‘page-attributes’ supported.

Thanks in advance for anyone’s help,

Adam

Solutions Collecting From Web of "Do attachments added to custom post types have a post parent?"

After some research, I found that Custom Post Types don’t store post_parent for attachments or may be requires some tweaking with register_post_type().

However, I found that when uploading attachments to regular posts, WordPress sends a post_id with AJAX and same is not happened with Custom Post Types. So, we need to assign post_id to Custom Post Type media uploader. Here is the snippet.

add_filter( 'wp_insert_post', 'foo_insert_post');
function foo_insert_post( $post_id, $post, $update ){
    //if this is cpt, go on
    if( 'your_cpt' === $post->post_type ){
        //ref: wp-includes\media.php @ ~2648
        //ref: https://developer.wordpress.org/reference/hooks/plupload_default_params/
        add_filter( 'plupload_default_params', 'foo_plupload_config');
    }
}

function foo_plupload_config($params){
    global $post;
    //assign current post id
    $params['post_id']      = $post->ID;
    return $params;
}

Hope, it works.

I recently had similar problem and I found that following option in register_post_type function:

"hierarchical" => true

is the answer.