I have O2M relationship between project prompt and project submission CPTs
Project prompts have a featured image but it is not required for project submissions to have an image.
I want this -
if project submission does not have a featured image during CRED submit, use the image from the project prompt
If project submission has an image during CRED submit, use that image.
I want to store this data in -_featured_image field in project submission and have tried to follow this thread - https://toolset.com/forums/topic/how-to-default-featured-image-field-to-that-of-the-related-post/#post-1506683
However, I am not sure how to pass the parent post featured image in the generic field of project submission CRED form. As the form is not submitted yet so how would the form know who is the parent post?
However, I am not sure how to pass the parent post featured image in the generic field of project submission CRED form. As the form is not submitted yet so how would the form know who is the parent post?
In that example, the parent post can be assumed because of where the Form is displayed. See the first comment:
'...I have a view for my "Lodge" post types, called "post-new-deal-form". In it, I display a form for adding a new "Deal" post...'
Lodge is the parent post type, and the child post Form is in the View of Lodges. So the parent is assumed to be whatever Lodge is displayed in the loop. That's how the parent can be determined before the Form is submitted in his case.
If your Form does not predefine the parent post somehow, you cannot use the same approach. No generic field is necessary. You would include the featured image field so the User can set the child post's featured image, but you would not include anything for the parent featured image field. The parent featured image could only be determined with PHP after Form submission.
You could use cred_submit_complete or cred_save_data to determine the parent post. The Post Relationship APIs like toolset_get_related_post and toolset_get_related_posts can be used in a cred_submit_complete hook, but not in a cred_save_data hook. The only way to get the selected parent post in a cred_save_data hook is to get the value of the corresponding post relationship field in the $_POST superglobal. That would be the parent post ID, which you can then use to call get_post_meta and get the featured image.
Christian,
Thanks for the feedback. So I followed your instructions and created the following code and it works. Leaving it here for other folks with similar issues.
<?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('cred_submit_complete', 'insert_featured_image',10,2);
function insert_featured_image($post_id, $form_data){
//insert the right form ID
if ($form_data['id']==6591) {
// $project_prompt_id = $_POST['@project-submission-to-expert-review-request_parent'];
$project_prompt_id = toolset_get_related_post($post_id,'project-prompt-to-project-submission', 'parent', array('post_status' => 'publish'));
if ( !has_post_thumbnail($post_id) && has_post_thumbnail($project_prompt_id)) {
$project_prompt_thumbnail_id = get_post_thumbnail_id($project_prompt_id);
set_post_thumbnail($post_id, $project_prompt_thumbnail_id);
}
}
}
?>
My issue is resolved now. Thank you!