Skip Navigation

[Resolved] Displaying data from a Parent post on a CRED form that creates a Child post

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

Problem:
How to display data or field from a Parent post on a CRED form that creates a Child post? I want to add a specific field from the parent post which is a Date field.

Solution:
1. Please add following code in your theme’s or child theme’s functions.php file:

add_shortcode('cred_parent_field', 'cred_parent_field_func');
  
function cred_parent_field_func($atts)
    {
        extract( shortcode_atts( array(
            'id'=>null,
            'field' => null,
            'format' => null
        ), $atts ) );
  
        $parent_id=null;
  
        if ( isset($_GET['parent_'.$id.'_id']) )
        {
            $parent_id=intval($_GET['parent_'.$id.'_id']);
        }
               return do_shortcode( '[types field="' . $field .'" format="'. $format . '" id="'. $parent_id . '"][/types]');
    }

2. Add this shortcode in your child post form:

[cred_parent_field field="assigned-hours" format="FIELD_NAME: FIELD_VALUE" id="program"]

In above shortcode:
==> Replace field="assigned-hours" with your field name
==> Replace id="program" with your Parent CPT slug
==> For Date field format, you can change format value as: format="m-d-Y"

This support ticket is created 7 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Noman 7 years, 4 months ago.

Assisted by: Noman.

Author
Posts
#545469

Hi there,

I am trying to display data from a Parent post on CRED form that creates a Child post.

I have found a lot of support tickets on this topic, but when I apply them, it does not work:
Getting the Parent post name on the CRED form for the Child post works fine by using: [cred_post_parent get='title']

But now I want to add a specific field from the parent post. It is a date field. I have tried this (as suggested in different other support tickets):
[types field="date" style="text" format="j F Y" id="[cred-post-parent get='id']"][/types]
This does not work.

Is there something else I could try or is it (as of now) just not possible to display other data than parent post title, parent post ID and parent post url?

Thanks in advance,
Michael

#545584

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Michael,

Thank you for contacting Toolset support.

1. Please add following code in your theme’s or child theme’s functions.php file:

add_shortcode('cred_parent_field', 'cred_parent_field_func');
 
function cred_parent_field_func($atts)
    {
        extract( shortcode_atts( array(
            'id'=>null,
            'field' => null,
            'format' => null
        ), $atts ) );
 
        $parent_id=null;
 
        if ( isset($_GET['parent_'.$id.'_id']) )
        {
            $parent_id=intval($_GET['parent_'.$id.'_id']);
        }
     
         return do_shortcode( '[types field="' . $field .'" format="'. $format . '" id="'. $parent_id . '"][/types]');
    }

2. Add this shortcode in your child post form:

[cred_parent_field field="assigned-hours" format="FIELD_NAME: FIELD_VALUE" id="program"]

In above shortcode:
Replace field="assigned-hours" with your field name
Replace id="program" with your Parent CPT slug

We have another client who had the similar issue and was also able to solve it:
https://toolset.com/forums/topic/how-to-display-parent-field-in-cred-form/#post-271696

Thank you

#545592

Hi Noman,

That did the trick. Perhaps - as a side note - the format argument is also one to consider. I wanted to show a date value so I also had to update the format. Perhaps this could be mentioned to others who have similar questions?

Thanks for the quick help 🙂

#545602

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes for the date field format we will use the shortcode something like this:

[cred_parent_field field="assigned-hours" format="m-d-Y" id="program"]