Adding a Post Thumbnail using Metabox Field on the Front End
Support › MB Frontend Submission › Adding a Post Thumbnail using Metabox Field on the Front EndResolved
- This topic has 7 replies, 2 voices, and was last updated 2 years, 12 months ago by
Warren Johnston.
-
AuthorPosts
-
May 7, 2022 at 12:11 AM #35920
Warren Johnston
ParticipantHi 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!
May 7, 2022 at 9:55 AM #35931Long Nguyen
ModeratorHi,
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-fieldsMay 7, 2022 at 3:09 PM #35934Warren Johnston
ParticipantHi 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
May 7, 2022 at 3:11 PM #35935Warren Johnston
ParticipantWhen 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.
May 8, 2022 at 12:36 PM #35944Long Nguyen
ModeratorHi,
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.May 8, 2022 at 2:55 PM #35947Warren Johnston
ParticipantThat 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
May 8, 2022 at 9:18 PM #35955Long Nguyen
ModeratorHi 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/
May 8, 2022 at 11:17 PM #35957Warren Johnston
ParticipantHere 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'] ); }
-
AuthorPosts
- You must be logged in to reply to this topic.