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
- Add a post when another post type is inserting
- Custom post type for ‘staff’ versus using wordpress user profiles?
- What are the differences between custom post type and custom page templates?
- How to get custom post type label and singular label from its slug?
- next_post_link() not working for single custom post type post
- How to set a default format for a custom post type?
- Now can I group custom post types together?
- Does anyone have any frameworks to setup custom post types and related actions/filters?
- echo value from ‘select’ field type into page template using cmb2?
- Exclude add_filter from the admin
- Undefined property: stdClass::$labels in general-template.php post_type_archive_title()
- Sanitizing post content for use in an email
- Genesis Child and Custom Post Type
- How to solve suspected memory issue in custom WordPress loop?
- Query multiple taxonomy in Custom Post Type
- automatically create taxonomy with same name as post title
- Separate Custom Post Type Taxonomy by Comma
- saving custom post type data to different table in wordpress
- Display “Post 2 of 4” on single post page?
- Query post types with multiple keys
- Create Custom Post Type Archive Page with Sub Categories Navigation Sidebar
- register_post_type – rewrite / feeds, what’s it for?
- WordPress import not importing custom taxonomy
- How to generate a sitemap with custom posts (not Google XML)
- Why are navigation items saved in the database?
- Filter posts with meta_query NOT IN where value has multiple values
- How to make post sticky in the admin page?
- WP 4.7 broke get_post_type or requires an explicit integer value?
- Remove base slug in permalinks of hierarchical custom post type
- How to show the archive / post type description on Single templates
- is_main_query() not working for WP REST API
- How can I move a custom taxonomy and its data from one post type to another?
- rewrite custom post type slug in wp admin
- Displaying the type of post (Custom) in a single.php
- Show listings from Impress Listing plugin in random order using taxonomy and terms
- If Statement – if current post is a child of a post with a taxonomy
- Get next and previous 3 posts in a term in single post page
- Can’t edit custom post type
- redirecting improperly after updating custom taxonomy term when referring from CPT edit page
- Deleting Custom Posts & Meta Data on Uninstall
- Register widget only page is a singular of custom post type
- I’d like to cycle through existing posts in a post type, showing one per day ordered by title, and starting over when the last is reached
- WooCommerce Customer Role Delete Custom Post Type
- Options page – dropdown of users
- Addition of custom option panel crashes Media Library & Admin Area
- Including metaboxes from custom post types in global search — continued
- CPT’s with landing page instead of archive – Url Rewrite?