Skip Navigation

[Resolved] Cred show group if grandparent is

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

Problem: I have two one-to-many post relationships that create a grandparent, parent, and child post. I have a Form that lets Users create child posts for a specific parent post. I would like to create a conditional group in the Form based on a field in the grandparent post.

Solution: Try this custom code in your child theme's functions.php file, or in a new snippet in Toolset > Settings > Custom code:

add_shortcode( 'ts-grandparent-field', 'ts_grandparent_field_func' );
function ts_grandparent_field_func( $atts ) {
  $a = shortcode_atts( array(
    'grandparent-parent' => null,
    'url-param' => null,
    'field' => null,
  ), $atts );
  if( !$a['grandparent-parent'] || !$a['url-param'] || !$a['field'] ) {
    return;
  }
  $parent_id = isset($_GET[$a['url-param']]) ? $_GET[$a['url-param']]: 0;
  if( !$parent_id )
    return;
  $grandparent_id = toolset_get_related_post( $parent_id, $a['grandparent-parent'], 'parent');
  if( !$grandparent_id )
    return;
  return get_post_meta( $grandparent_id, 'wpcf-' . $a['field'], true);
}

Then you can use it like this:

Class info: [ts-grandparent-field grandparent-parent="class-starting" url-param="parent_starting_id" field="class-info"][/ts-grandparent-field]

100% of people find this useful.

This support ticket is created 6 years, 2 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)

This topic contains 4 replies, has 2 voices.

Last updated by sarahK-2 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1169041

I am building an add child form with a known parent.

I have used a add child post link on the parent's page. The grandparent is also known, and I would like to show certain different field groups on the form dependent on the child post's grandparent.

I am sure this is simple and I have followed the documentation but I am clearly just not getting something right.

I want the correct way to write this to show the group based on the name of a particular grandparent field (or category would also be fine)

[cred_show_group if="******************************************" mode="none"][/cred_show_group]

Thanks in advance!

#1169818

Hi, it's actually not simple to access grandparent post information in this case. The recommended way to access grandparent post information is to nest Content Templates:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-fields-of-grandparents/
However, in your case you need to use the output of that template, based on the parent ID in a URL parameter, in the condition of a conditional form group. There are limits to the levels of nested shortcodes, I don't think you'll be able to nest enough levels of shortcodes to make this work. I think you will need a single custom shortcode that accesses the parent post ID from the URL parameters, then returns the value you want to test from the grandparent post. I might be able to help if you tell me:
- What is the slug of the parent-child relationship?
- What is the slug of the grandparent-parent relationship?
- What is the URL parameter used to pass the parent ID to the new child Form? You can just copy + paste the full URL after you click the Add Child Link from the parent post.
- What is the slug of the custom field in the grandparent post?

#1170064

- What is the slug of the parent-child relationship?

starting-registration

- What is the slug of the grandparent-parent relationship?

class-starting

- What is the URL parameter used to pass the parent ID to the new child Form? You can just copy + paste the full URL after you click the Add Child Link from the parent post.

hidden link

- What is the slug of the custom field in the grandparent post?

class-info

*********************
Thanks! Looking forward to seeing what you can do, and let me know what else you need.

#1170709

Try this custom code in your child theme's functions.php file, or in a new snippet in Toolset > Settings > Custom code:

add_shortcode( 'ts-grandparent-field', 'ts_grandparent_field_func' );
function ts_grandparent_field_func( $atts ) {
  $a = shortcode_atts( array(
    'grandparent-parent' => null,
    'url-param' => null,
    'field' => null,
  ), $atts );
  if( !$a['grandparent-parent'] || !$a['url-param'] || !$a['field'] ) {
    return;
  }
  $parent_id = isset($_GET[$a['url-param']]) ? $_GET[$a['url-param']]: 0;
  if( !$parent_id )
    return;
  $grandparent_id = toolset_get_related_post( $parent_id, $a['grandparent-parent'], 'parent');
  if( !$grandparent_id )
    return;
  return get_post_meta( $grandparent_id, 'wpcf-' . $a['field'], true);
}

Then you can use it like this:

Class info: [ts-grandparent-field grandparent-parent="class-starting" url-param="parent_starting_id" field="class-info"][/ts-grandparent-field]
#1171663

Thank you, Christian. That is a perfect solution, and very easy for me to implement.

I'm so glad I asked 🙂