Skip Navigation

[Resolved] How to insert parent featured image to child post if no child post has no image

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that creates child posts. If the featured image is not provided in the child post Form, I would like to copy the featured image from the parent post into the featured image of the child post.

Solution: If the parent post is not predefined, then you must use the Forms API to copy the parent post's featured image into the child post's featured image since the parent post cannot be determined at the time the Form is loaded. Use the cred_submit_complete API to automate the process. In that callback you can use toolset_get_related_post or toolset_get_related_posts to query the parent post, then get_post_meta to fetch the featured image, then update_post_meta in the child post to set the featured image programmatically.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 3 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#2120667

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?

#2121249

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.

#2121495

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);
    }
  }
}  
?>
#2121497

My issue is resolved now. Thank you!