Hi, I need help with assigning featured image from custom image field.
I made the code from here to work - https://toolset.com/forums/topic/use-first-uploaded-image-as-featured-image/ ONLY if WordPress Media Library manager is checked.
However I cannot use it, since I cannot limit the amount of images user can select, even If I hide the "add more button".
I've tried this code, but it's not working.
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_action('pending_to_publish', 'update_featured_image',10 ,3);
function update_featured_image($post_id){
global $post;
global $wpdb;
if (($post->post_type=='post') && (!has_post_thumbnail($post_id)) ){
$images = get_post_field( 'wpcf-more-pic', $post_id );
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $images ));
set_post_thumbnail($post_id, $attachment[0]);
}
}
My form ID is "5645" and the field is "more-pic".
Can you please help.
Hi there,
It is not possible to know what is wrong with your implementation as it is not related in any shape or form to the suggested code by my colleague on the ticket you mentioned.
The context of that ticket was to use the Form save data hook to achieve the featured image status.
You use the "pending_to_publish". I suggest you first use the form for edit or add and add the code in "cred_save_data" hook.
Thanks.
Hi, Christopher,
I suppose I didn't explain it properly.
I made the code with the link WORK, but ONLY when I check WordPress Media Library manager, and I need that to work without it. I mentioned it so show what I've tried so far, in case it's of use.
The code I'm following is this thread - https://toolset.com/forums/topic/generating-featured-image-from-post-forms-image-upload-multiple-or-single/ , but no luck so far.
The idea is to update/add the "featured image" after the post has been created, from the first uploaded image of that repeatable field.
Can you help?
Hi there,
I am not aware of any method to be able to handle that as the feature needs Ajax as mentioned by my colleague on the ticket you have mentioned.
The other solution given by the customer is not tested by us and the context of that is the customer had the posts set to pending. And then he used the code to set a featured image when he manually change the status to published.
I do not think it is possible to do that exactly while the form is submitted. You can test by using the same code but instead attaching the function to this hook and see if it works:
publish_post
Or you use the draft_to_publish method if you decide to set the initial creation of the post to be a draft and when you set it to a published post from admin it selects the correct image. (Again on this scenario the page is already created and WordPress has access to the image to be able to add it to a featured image)
I can ask the second tier support to see if there is a workaround but for that I need to you login to this sample installation and set up a quick demo with the form and the repeatable field so I can give the report and ask:
hidden link
Thanks.
Hi,
I've added both the code and the field, however I cannot add the custom form in the demo.
I've tried the hook, but it does not work.
Unfortunately, the website should work like this: user uploads post for sale, automatically the post goes published and shows in search view.
The good UX should be - in the upload form to use a single repeating button and upload x amount of images per post.
However, I cannot limit the WP media manager selected images, so I have to use the form with that unchecked. This way the featured image becomes the first uploaded from that field.
Since this does not work with the codes so far, my other option is to use 5 different image fields and one of them to be the real featured image and go to bad UX.
Please, can the second tier support help.
Hi there,
I will ask the second tiers support if there is a way, bt the problem you had in the sandbox installation was that the Toolset Form plugin was not active.
I activated it an dyou can add the proper form so I can show the second tier support.
Hi,
I've added everything. Hopefully, someone can get it to work, since I'm bad with code myself and cannot do it.
Thanks
The question is referred to the second tier support.
An answer will be available next week as we are on a weekend period.
Thanks.
Hi there,
Our awesome second tier support has an answer. Please add the code below:
add_action( 'cred_save_data', 'tssupp_make_featured_image', 10, 2 );
function tssupp_make_featured_image( $post_id, $form_data ){
$image_field = 'pictures'; // edit field slug
if ( in_array( $form_data['id'], array( 123 ) ) ) { // edit form ID
// get the images
$images = get_post_meta( $post_id, 'wpcf-'.$image_field, false );
// get the attachment ID of the first image
if ( is_array($images) ){
$first = attachment_url_to_postid( $images[0] );
if ( $first > 0 ){
// set the featured image
update_post_meta( $post_id, '_thumbnail_id', $first );
// delete the first custom field image
delete_post_meta( $post_id, 'wpcf-'.$image_field, $images[0] );
}
}
}
}
Replace "id" with the form ID and replace "pictures" with the repeating custom field that you use for the images.
The working code also can be seen in the sandbox installation:
hidden link
Thanks.
I've noticed that when I use the image slider, the first image (featured image from the repeating field) does not show.
However it shows when viewing the post in the search.
I fixed it myself. Posting the solution, in case someone wants to use the same fields.
Remove this part from the code above:
// delete the first custom field image
delete_post_meta( $post_id, 'wpcf-'.$image_field, $images[0] );