Last active
February 21, 2018 03:48
-
-
Save juyal-ahmed/31d8a1c6c8576f3b18f6 to your computer and use it in GitHub Desktop.
Upload featured image on custom post from WordPress front-end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Just uploading photo or attachments | |
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/file.php'); | |
require_once(ABSPATH . "wp-admin" . '/includes/media.php'); | |
$file_handler = 'upload_attachment' //Form attachment Field name. | |
$attach_id = media_handle_upload( $file_handler, $post_id ); | |
//making it featured! | |
set_post_thumbnail($post_ID, $attach_id ); | |
or | |
update_post_meta($post_id,'_thumbnail_id',$attach_id); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andwp_postmeta
tables. But when I go to admin to check the post in edit mode I am unable to see the image underFeatured Image
section.The other thing I noticed the upload routine actually created a new
ID
inwp_posts
table and the attachment information inwp_postmeta
table got saved against this ID only, not against my actualpost_id
. Probably this is the reason why the image is not coming up in admin edit page forpost_id
I am editing.Summary:
Actual post ID in
wp_posts
table: 434Attachment saved in
wp_posts
table with ID: 435Attachment meta information saved in
wp_postmeta
table againstpost_id
: 435Here 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?