1

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

  • Are the links to the post you uploaded the attachment to at the media library overview correct? Besides, don't worry, you know the invisible hand will take care of it :) You surely never heard that one before :D – Nicolai Apr 17 '15 at 15:53
  • Looking for an answer, I have created a CPT and all attachments to it don't have post_parent. – Nishant Kumar Jul 20 '15 at 5:31
  • You are probably doing smth wrong. Just checked on my several sites using CPTs - everywhere there's a correct post_parent for attachments, uploaded to custom post type. The thing is, when uploading attachment, it uses post ID, and it does not matter if it's default post, page, or custom post type, it's just any entry from wp_posts table. There must be smth wrong on your side. – Ihor Vorotnov Jul 20 '15 at 11:44
1

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.

1

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

"hierarchical" => true

is the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.