Your current code should be correctly creating the post inside your Products post type.
You’re missing the bit that updates the post meta holding the image. Assuming the meta_key is product_image
, then running this on success should do:
update_post_meta( $pid, 'product_image', $attachment_id );
on a side note: don’t forget to run wp_generate_attachment_metadata and wp_update_attachment_metadata after you upload the image.
Related Posts:
- Get post ID from wp_insert_post()
- WP insert post and custom taxonomy
- How do I programmatically add items of content to a custom post type?
- Not Able to Insert Taxonomy Term Using wp_insert_post()
- How I upload, save and set a featured image from my frontend?
- Trying to save custom post type from frontend partially working
- wp_insert_post custom taxonomy
- Assign category to front end post
- How can I programmatically save data into custom fields that contain serialized data?
- What hook should be used to programmatically create a post only when master post is updated?
- WordPress tax_input only if logged in
- Programmatically adding posts
- Is it possible to create a post using a metabox?
- How Can I Set the Post Author of a Post I Just Created With PHP?
- How to auto increment post title & post slug field?
- Auto-generated posts not showing in backend (but being counted!)
- wp_insert_post generates endless posts
- create parent post using wp_insert_post
- Cron job for creating posts not excecuting properly
- How to checked selected category checkbox when my custom post inserted from frontend
- Post author for revision not being set on update/insert
- Add CPT values to Database
- Why cant I add a custom post type to a custom taxonomy?
- wp_insert_post wrong post type [closed]
- Programatically inserted posts not showing in Posts Table
- Is it possible to add posts and images (auto resize) for custom post types with a script?
- Update post if exist from PHP
- WordPress hook which triggers on post import
- How to handle new post from API request?
- How to handle this specific case of custom post type?
- importing table data to custom post type
- How can I get some of a posts meta data whilst it is still inserting?
- See if the email exists or not
- WP_Insert_Post creating duplicate posts when logged in
- wp_insert_post from XML feed only inserting first post
- What is the correct way that when creating a custom post type assign values to custom fields created with pods framework?
- How do I find out which (page) template file my custom child post is looking for?
- wp_insert_post deleting previous post custom meta
- Published page but it is page not found
- How to insert post 6 times after user register like ask.fm
- wp_insert_post() creates duplicates with ‘post_status’ => ‘publish’
- automatic creation of custom post type articles
- Submitting custom post from frontend,jQuery ajax, and custom validation – can’t find proper place to insert wp_insert_post()
- UPDATED: Save a custom_field value when automatically creating a post using wp_insert_post
- Generate slug and meta data if meta field is empty
- Add a image to a post published on a CPT from the front-end
- wordpress form processing to custom post type not working
- install.php Custom Taxonomy Term not being added to custom post
- Omitted Content After Post-Publish Insertion
- Is this the correct way to add post-slug input field?
- wp_insert_post not working for custom post type?
- After wp_insert_post() custom post type does not show in the admin
- Using wp_insert_post to create custom posts with ACF image field
- tag.php doesn’t work with tags on a custom post type post?
- If featured image doesn’t exist, show post content
- Getting a div content of a post in wordpress?
- Rename a current used post type
- Include custom post types in categories widget
- Custom field losing p tags on display
- How to customize admin posts based on the user who is logged in
- Custom post type – how can I make it a sub-url of another page?
- apply filters only to specific post listing without check the url parameters
- URI returns a post when it should return a 404
- Twenty Fifteen: Adjacent posts by menu_order [duplicate]
- Change meta box when the page’s template is changed
- Custom Post Type Loop throws 500 error when used in widget
- Define new user capability for custom post types?
- limit value taxonomy based on previous taxonomy value wordpress
- pre_get_posts for custom page builder meta box
- Pre-styling post content to display in lightbox
- Get Post Types in admin
- Create metaboxes based on custom post type
- custom post type and custom taxonomy
- Custom post type as child of page
- Conditional Tags for specific category of custom posts
- populate array with posts
- Transferring data to the end of a transaction with the Shopp Plugin
- Listing custom post types on archive page with array
- Get custom post type categories to show up in menus
- Query all post types but limit to parents
- get post count for related author
- What’s an easy way of sorting custom post types manually?
- How to Implement Search Functionality?
- Adding /blog in front of single posts (only)
- wp rest api orderby field in a custom table
- Post template with breadcrumb needs to show page navigated from
- Custom post types – meta_query: search lesson which starts sooner
- Saving custom post types post_meta over REST-API fails
- WordPress Custom Post Type – Post Attribute: Template. Template shows up and saves on the back end, but the default theme file is being rendered
- page not found for example.com/custom-post-name
- How to show Y number of custom posts after every X normal posts?
- Display Custom Post if custom field is marked
- List a custom post type’s posts ordered by nested custom taxonomy
- listing custom post type category page
- Inserting random posts
- Allow authors to create article image
- Rewrite a filter as shortcode (or something like that) to use anywhere in CPT
- How to delete unnecessary custom post types in the UI
- Assign a tag to custom post type using a query
- Categorizing Custom Posts in Bulk Based on Title