Skip Navigation

[Resolved] How to display the child value in a parent (list) view?

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

Problem:
How to display conditional data inside views based on child post custom field values

Solution:
We can write custom shortcode to check if a particular post has child post and if that has child post we will get custom field values of child post using "get_post_meta()" function.

Code : https://toolset.com/forums/topic/how-to-display-the-child-value-in-a-parent-list-view/page/2#post-371431

Call the shortcode:

[has_children_of_type post_type="aanmelding" ]

Where:
"aanmelding" is your custom post type for which you would like to fetch child post.

Relevant Documentation:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

This support ticket is created 8 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.

Our next available supporter will start replying to tickets in about 1.72 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by mcjS 8 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#370100

I am trying to: Display the value of a child field (there is always one child with the parent)

I visited this URL: https://toolset.com/documentation/user-guides/displaying-related-child-posts/#3

I expected to see: the raw value of the field

Instead, I got: nothing

this is part of my code in the Template for this View / loop item (item = parent)

***

[wpv-conditional if="( '[has_children_of_type post_type="testimonial" ]' eq '1' )"]
[types field="option" output="raw"][/types]
[/wpv-conditional]

***

"option" is a child-field in testimonial.

p.s.
I can not use the child-fields in the parent-type. The parent has to show always, the child value only appears when a payment is completed.

#370161

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To check if parent post has child post-field you need to add custom shortcode/function to check. Please refer to the following documentation:
=> https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

For example, What you can do is to create a Custom ShortCode where you perform a check if parent post has child post.

You can also use the Types API to build this query, such a ShortCode can look like this:

function child_exists_func( $atts ){
$child_posts = types_child_posts('your-child-post-type-slug-here');
if ($child_posts) {
return true;
}
}
add_shortcode( 'child-exists', 'child_exists_func' );

Where:
Replace 'your-child-post-type-slug-here' with your child post slug.

This above ShortCode would be used on the PARENT post type and returns true if a Child exists.
Of course, you can extend this to return the ID, or whatever you want of the Child Post
Also, you should include a case for if no child exists, as example you can return false.

This ShortCode can then be registered in Views > Settings and be used in the Views Conditional Output.
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/ >Third-party shortcode arguments

I hope this solution will help you to resolve your issue.

#370311

I already did this and this is working:
"For example, What you can do is to create a Custom ShortCode where you perform a check if parent post has child post."

otherwise this condition wasn't working:
[wpv-conditional if="( '[has_children_of_type post_type="testimonial" ]' eq '1' )"]

What do you mean by "Of course, you can extend this to return the ID, or whatever you want of the Child Post"

I'm looking for something inside the wpv-conditional to display the value of a child-field. And this is the part I have problems with.

#370465

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What do you mean by "Of course, you can extend this to return the ID, or whatever you want of the Child Post"
=> I mean to say currently we return 'true' or 'false' based on the condition that if post has child or not. You can return any value based on your requirement.

I think you should refer to following documentation that may help you:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#370489

I read the documentation, but I don't see a sample of something like

[wpv-conditional if="( '[has_children_of_type post_type="testimonial" ]' eq '1' )"]
display the child-value of the child-field-option
[/wpv-conditional]

I know wpv-conditional is given true and then I want to display a child-value.
is it something like
[types child-field="option" output="raw"][/types]
?

i'm not a php coder , so I'm looking for help to make my code working.

#370541

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please share problem URL as well as which view you are using?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#370957

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please let me know the complete range of this condition:

IF there is a child (AANMELDING)
display the field option (value)
if the child-field option = 5104 then display "zaterdag"
if the child-field option = 5106 then display "zondag"
.etc
.etc
.etc
if the child-field option =7625 then display "zaterdag en zondag"
END

means how many combinations you have lets say "child-field option = 5104 then display "zaterdag""
then if child field option = xx then display yyyy.

#371010

if the child-field option = 5104 then display "zaterdag 30 april"
if the child-field option = 5106 then display "zondag 1 mei"
if the child-field option = 5107 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 5144 then display "zaterdag 30 april"
if the child-field option = 5145 then display "zondag 1 mei"
if the child-field option = 5146 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 5147 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 7622 then display "zaterdag 30 april"
if the child-field option = 7623 then display "zondag 1 mei"
if the child-field option = 7624 then display "zaterdag 30 april en zondag 1 mei"
if the child-field option = 7625 then display "zaterdag 30 april en zondag 1 mei"

this is the complete list.

#371343

Can you give me information about the status?
When I take a look now, the (incomplete) message wich worked before, isn't showing anymore?

[wpv-conditional if="( '[has_children_of_type post_type="aanmelding" ]' eq '1' )"]
<div class="blue">some text</div>
[/wpv-conditional]

#371391

my mistake
"
When I take a look now, the (incomplete) message wich worked before, isn't showing anymore?

[wpv-conditional if="( '[has_children_of_type post_type="aanmelding" ]' eq '1' )"]
<div class="blue">some text</div>
[/wpv-conditional]
"

it is showing.

But can you inform me about the status?

#371395

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi there,

FTP access details are not working at this end. Could you please resend it to me.

#371409

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I tried to access the view you shared with me before it says its been deleted:
hidden link

Could you please tell me which view I should look? and which is your problem URL now?

#371416

hidden link
(Templates for this View) = hidden link = homepage
and
hidden link
(Template) = when you click on "lees meer" = more information at an ad.

Excuse me, but I had to work on it by the weekend.My mistake.

#371431

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've modified the code in your functions.php file as given under:

function has_children_of_type( $atts ) {
    global $post;
    extract( shortcode_atts( array(
        'post_type' => 'advertentie', // Make sure to set the correct post type
    ), $atts ) );
    $parent_type = $post->post_type;
    $childargs = array(
        'post_type' => $post_type,
        'numberposts' => -1,
        'meta_query' => array( array( 'key' => '_wpcf_belongs_'.$parent_type.'_id', 'value' => $post->ID ) )
    );
    $child_posts = get_posts( $childargs );
	if(count( $child_posts )){
		
		$x_value = array('5104'=>'zaterdag 30 april',
						 '5106'=>'zondag 1 mei',
						 '5107'=>'zaterdag 30 april en zondag 1 mei',
						 '5144'=>'zaterdag 30 april',
						 '5145'=>'zondag 1 mei',
						 '5146'=>'zaterdag 30 april en zondag 1 mei',
						 '5147'=>'zaterdag 30 april en zondag 1 mei',
						 '7622'=>'zaterdag 30 april',
						 '7623'=>'zondag 1 mei',
						 '7624'=>'zaterdag 30 april en zondag 1 mei',
						 '7625'=>'zaterdag 30 april en zondag 1 mei',
						 
						 
						 );
			
		
		$x = get_post_meta($child_posts[0]->ID,'wpcf-optie',true);
		if(is_numeric($x)){
			return $x_value[$x];
		}
			
		
	}
    //return count( $child_posts ) == 0 ? false : true; 
}
add_shortcode( 'has_children_of_type', 'has_children_of_type' );

And I've called it above shortcode from your view template as given under:

[has_children_of_type post_type="aanmelding" ]
#371442

The problem now is: every ad is showing the standard custom text (blue "highlighted") it is only needed when there is a child. Otherwise it is wrong to show the standard custom text.

I'm missing now the wpv-conditional in the view?

[wpv-conditional if="( '[has_children_of_type post_type="aanmelding" ]' eq '1' )"]
<div class="blue" style="margin-top: 9px">standard custom text (plus the option description)</div>
[/wpv-conditional]

I mean with "(plus the option description)" the value of the array.

?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.