Support Forum
Support › MB Frontend Submission › Adding a Post Thumbnail using Metabox Field on the Front EndResolved
Hi Guys
I'm trying to keep all my fields for post creation on the front end as metabox fields (for security and the ability to use conditions etc)
Im using this code to add Test to the Post Title Field and it works great
add_action( 'rwmb_project-core_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$my_meta = rwmb_meta( 'text_g9i37hn241o', '', $post_id );
// Preprare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $my_meta,
);
wp_update_post( $my_post );
}
How would I go about setting the thumbnail using a Metabox Single Image Upload field?
Many thanks again in advance!
Hi,
If you just want to use the field single_image
on the frontend submission form and don't want to use it on the backend, you can set the field ID to _thumbnail_id
to set it as a featured image, and no need to use the custom code to update the featured image. Read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/#reorder-post-fields
Hi Long
That is not working mate, I've tried that multiple times.
The only thing I can think of is my field group is being stored as a custom table - would that make any difference.
Happy to set you guys up with an admin account if you want to take a look?
Warren
When I browse to the post in the back end I do see the image in the field group, but the featured image box in the side bar is empty. So its saving the image to the table but not adding it as the featured image for the custom post.
Hi,
The featured image of the post saved data to the standard table wp_postmeta
so it does not work if you save the custom field value to the custom table. You can try to save the field value to the standard table and re-check this case.
That makes sense
Would you guys have a recommended piece of code like the above based on _after_save_post that would attach the image to to the post featured image correctly?
Many thanks
Warren
Hi Warren,
You can use the WordPress function set_post_thumbnail()
to set the featured image for the post. For example:
function update_post_thumbnail( $post_id ) {
// Get the field value
$my_meta = rwmb_meta( 'single_image', '', $post_id );
set_post_thumbnail( $post_id, $my_meta );
}
Read more on the documentation https://developer.wordpress.org/reference/functions/set_post_thumbnail/
Here is some code for future reference for anyone struggling with this 🙂
add_action( 'rwmb_XXXFIELDGROUPIDXXX_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$my_title = rwmb_meta( 'XXXFIELDIDXXX', '', $post_id );
$my_desc = rwmb_meta( 'XXXFIELDIDXXX', '', $post_id );
$my_feature = rwmb_meta( 'XXXFIELDIDXXX','', $post_id );
// Prepare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $my_title,
'post_excerpt' => $my_desc,
);
wp_update_post( $my_post );
set_post_thumbnail( $post_id, $my_feature['ID'] );
}