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.
Related Posts:
- Creating an Image-Centric Custom Post Type?
- Submit post and upload image from front-end
- Correctly delete posts with meta and attachments [duplicate]
- Attaching media to custom posts without editor
- get attachments for all posts of particular post type
- List most recent image uploads, but only for specific custom post type
- wp_generate_attachment_metadata gives me an empty array
- Add custom field to media attachment image attribute in post editor
- Count all images of a certain post type
- How do you modify the ‘post_parent’ of a custom post type?
- Stopping WordPress from Auto Generating Image Files for Sizes
- Return Attachments from Custom Post Type
- How to get attached file in wordpress from custom post
- Impossible to get Attachments Outside WordPress?
- Issue to get wp_get_attachment_image with cmb2
- Custom Post Type – List all attachments on Edit Screen
- Disable featured image
- get_attached_media() on author page not working
- Why can’t my admin account manage attachments on a custom post type with a unique capability type?
- How can I get the number of custom post type posts that have a specific attachment image set?
- Hierarchy and access control for Custom Post Types (CPT)
- Searching Custom Post Types
- How to import files to individual posts of a custom type
- How do you get the full size url of an attachment image
- Add post meta programmatically to attachment
- Show list of custom posts with respective attachments?
- How do I add html content to custom post type Posts using php?
- Let users upload image(s) to the post from front end
- How do I add multiple post types to this query?
- Why doesnt my tag page populate with this custom post type?
- How to add a default predefined thumbnail when creating a brand new post?
- Custom attachment function not working in v5.4.2?
- Issue on Getting URL of Uploaded Image to a Custom Post Type
- Dupplication of Custom Post Type while adding attachment
- Update Cateogory Taxonomy Count for Attachment Post Type
- Delete custom post type metadata without deleting the post in admin area
- Delete attachment with post
- Display custom post type attached media file sizes
- set_post_thumbnail_size in percent, not pixels?
- How to query for posts (in hierarchical custom post type) that have children?
- 404 error PageNavi custom type taxonomy | wordpress
- Registering tags taxonomy for a custom post type
- Message on Custom Posts Type admin page
- Why do my custom post types show up in the dashboard, but not on my site?
- Set meta_query only for specific post type
- Get the next event using date_query
- How to make in WordPress admin panel sortable column for the custom field, that count the number of page impressions?
- Export Custom Post Type posts with all Custom Fields and Custom Texonomy associated with it
- How to Combine Two Custom Post Types into Single Permalink Structure
- Displaying a custom post types custom taxonomy value?
- wpdb custom post_type problem
- How to rank custom post type from score points
- Custom Post Type Not working like a Post?
- Show the same Article Available in Other Categories
- Custom Post Type Taxonomy Filters
- Show custom post archive when custom post not specified
- Show custom post type relationships by taxonomy
- register_post_meta show “Null”
- get_post_meta not working on publishing
- Remove Add Media and all Rich text editor buttons?
- Custom Posts on homepage
- Conditional statement for if archive page has posts which contain certain taxonomies/categories/tags, show those terms
- Custom Post Conditional for Parent & Child Single.php?
- Different Category system needed for the Custom Post Type
- Autocomplete or suggest from post titles inside plugin
- How to make a posttype show under a page hierarchy (example.com/page/posttype/entry)?
- Convert post type to another
- WordPress wp_query() basic question about args
- List custom taxonomy as navigation – taxonomy pages with all it’s posts?
- How can I set a meta value that will then affect other post meta values?
- Get a list of all custom post type names/slugs [duplicate]
- WP_Query get post from a category and from another post type
- Slider from custom post type
- Custom Post Type View Button in Admin Gives 400 Error
- Extra richtext admin textfield for custom posttype with toolbar tabs?
- either single-{custom}.php 404’s OR /taxonomy/ 404’s – custom post type and taxonomy permalinks
- How do I list the next 7 days and any events (cpt) contained in those days
- Custom Post Types Not Showing Up In query_posts Result
- Do I need a loop to display an object that attached to posts?
- How to get next post link of child custom post type from parent post and get next post link of parent post from the last child post?
- Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
- Select custom post type do display from page editor
- Get Custom Post types data from the API
- CPTs relationship
- Search only one custom post type
- If ACF Post Object post has custom taxonomy term…
- How to filter taxonomy of a custom post type and display it in the single post type page in the back end?
- Parent and child categories with their id in drop_down in CPT
- sorting in wp query based on custom field value
- WooCommerce breadcrumb display custom posts instead of product data
- Include images from pages in wp search.php results in default wp search
- Custom post type post_type_link gives “page not found” on any other custom post type
- Custom Post Type shows pagination (w/404) or posts_per_page query, but not both
- getting a blank login page :( any way I can fix this without having to do a clean install?
- Page to show custom posts and one category
- Custom Search not working
- Pagination not working with custom loop
- Hide custom post type and use its slug in new plugin menu
- Can WordPress show posts based on a button that the user clicked 2 pages back?
- Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working