WordPress GuideErrors → Custom Post Not Showing Parent

Fixed: WordPress custom post type attachment not showing parent

When working with custom post types in WordPress, one frustrating issue you might face is attachments not showing their parent post relationship. Images or PDFs appear in your Media Library, but they’re not linked to any post—even when you uploaded them while editing a custom post type. 

Here’s what’s going on and how to fix it.

What this issue looks like

You’ll usually notice something’s wrong in one of these ways:

This happens most often with custom post types (CPTs), especially when using advanced metaboxes or custom upload workflows.

Why WordPress attachments lose their parent in custom post types

By default, WordPress assigns a post_parent value to attachments when you upload them through the Classic or Block Editor of a post or page. But with custom post types, that doesn’t always happen.

Here’s why:

Without an explicit post_parent set, the attachment ends up “orphaned” even if it was uploaded from a specific custom post.

Step-by-step fix: assign attachments to their custom post type parent

The most reliable way to fix this is to make sure attachments are programmatically assigned to their parent post during upload.

1. Make sure your CPT supports relevant features

When registering your custom post type in functions.php or a plugin, confirm it includes the right supports:

‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’, ‘custom-fields’),

Without editor or thumbnail, the post edit screen may not behave like a standard post or page—and that can block the attachment-parent logic from triggering.

2. Automatically assign attachment parent on upload

Here’s how to hook into the upload process and assign the current post ID as the attachment’s parent.

add_action(‘add_attachment’, ‘assign_attachment_to_cpt_parent’);
function assign_attachment_to_cpt_parent($attachment_id) {
    if (!isset($_REQUEST[‘post_id’])) return;
    $parent_id = intval($_REQUEST[‘post_id’]);
    $parent_post = get_post($parent_id);
    // Only assign parent if it’s a specific custom post type
    if ($parent_post && $parent_post->post_type ===
‘your_custom_post_type’) {
        wp_update_post(array(
            ‘ID’ => $attachment_id,
            ‘post_parent’ => $parent_id
        ));
    }
}

This code runs when a file is uploaded and:

Replace ‘your_custom_post_type’ with your actual post type slug.

If you’re using a custom uploader that doesn’t pass post_id, you’ll need to add that logic manually or configure the uploader to include it.

Optional: fixing broken links in the front end

Even if the parent relationship exists, it won’t always display the way you expect unless your theme or shortcode handles it correctly.

Use wp_get_attachment_link() with fallbacks

$post_id = get_post_field(‘post_parent’, $attachment_id);
if ($post_id) {
    echo wp_get_attachment_link($attachment_id);
} else {
    echo wp_get_attachment_link($attachment_id, ‘thumbnail’, false);
}

This fallback ensures that attachments still render even without a valid parent.

Adjust gallery shortcodes or queries

Some gallery systems or

shortcodes rely on the post_parent. You can override that by specifying IDs manually or querying attachments with custom logic:

$attachments = get_posts(array(
    ‘post_type’ => ‘attachment’,
    ‘post_parent’ => $custom_post_id,
    ‘posts_per_page’ => -1
));

Or use post__in with custom fields if attachments are saved to post meta instead of post_parent.

Alternative methods: plugins that manage media relationships

If you prefer not to mess with code, here are some well-rated plugins that help manage attachments and parent relationships:

Neither of these plugins will magically reassign post_parent, but they give you more control and visibility over media relationships.

Use case: when this fix matters most

Attachment-parent relationships are important when:

For example, a real estate site using a “Listing” CPT might attach property images to each listing. Without parent links, image galleries won’t populate automatically, and plugins that depend on those links won’t work.

Additional resources

Diagnosing WordPress errors on your site →

Even more common errors, how to troubleshoot them, and how to solve them

Fixed: WordPress failed to write file to disk →

Learn how to fix the “Upload: Failed to Write File to Disk” error in WordPress with easy troubleshooting steps.

What is managed WordPress hosting? →

What it means, what it includes, and how to decide if it’s right for you