Skip to content

Instantly share code, notes, and snippets.

@tojibon
Last active February 21, 2018 03:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tojibon/31d8a1c6c8576f3b18f6 to your computer and use it in GitHub Desktop.
Save tojibon/31d8a1c6c8576f3b18f6 to your computer and use it in GitHub Desktop.
Upload featured image on custom post from WordPress front-end
@subtata-emfluence
Copy link

subtata-emfluence commented Feb 16, 2017

Hi,
I have tried your above code snippet in my custom post type front end form. It looks like the image I uploaded via the form got uploaded as I am able to see it in media library. It also created entries in wp_posts and wp_postmeta tables. But when I go to admin to check the post in edit mode I am unable to see the image under Featured Image section.

The other thing I noticed the upload routine actually created a new ID in wp_posts table and the attachment information in wp_postmeta table got saved against this ID only, not against my actual post_id. Probably this is the reason why the image is not coming up in admin edit page for post_id I am editing.

Summary:
Actual post ID in wp_posts table: 434
Attachment saved in wp_posts table with ID: 435
Attachment meta information saved in wp_postmeta table against post_id: 435

Here is my code block:

`<?php
get_header();

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

// grabbing form values here...

$post_args = array(
'post_title' => $title,
'post_content' => $story,
'tax_input' => array($story_type),
'post_type' => 'travelog',
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'closed'
);

//Save main post and grab last inserted post_id
$post_id = wp_insert_post($post_args);

//Save meta information against last inserted post_id
add_post_meta($post_id, '_travelog_year_visited', $year_visited);
add_post_meta($post_id, '_travelog_adults_count', $adult_count);
add_post_meta($post_id, '_travelog_children_count', $child_count);
add_post_meta($post_id, '_travelog_trip_cost', $trip_cost);
add_post_meta($post_id, '_travelog_additional_info', $addl_info);
add_post_meta($post_id, '_travelog_places_covered', $places_covered);

//Save term relationships against last inserted post_id
wp_set_object_terms($post_id, array((int)$category), 'category');
wp_set_object_terms($post_id, array((int)$story_type), 'story-type');

//Upload featured image / attachment
$file_handler = 'travelog_thumbnail'; //Form attachment Field name.
$attach_id = media_handle_upload( $file_handler, $post_id );

// $post_id in above line is the last inserted post_id which I am grabbing from this line:
$post_id = wp_insert_post($post_args);

get_footer();
?>`

Why attachment is saving as a new post when I have specified which post_id to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment