Skip Navigation

[Resolved] Conditional output based on child post

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

Problem:
Conditional output based on child post

Solution:
To display conditional content based on the child posts with pre-Types 3.X.X, you need to use custom shortcode with [wpv-conditional] shortcode.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/conditional-output-based-on-child-post/#post-921710

Relevant Documentation:

This support ticket is created 6 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
- 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 15 replies, has 2 voices.

Last updated by Pat 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#921270

Pat

Hello,

I have a postype "objet" which is parent from a postype "wishlist"
In the "objet" content template, I need to define a conditional output based on the following statement :
- a child "wishlist" post exist for the displayed objet
- it has the same author than the current logged in user

Can you help on this as I have not been able to find the right way to make it!
Thanks
Pat

#921658

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you can use following shortcode to check if any child post exists for your currently displayed parent.

Add following shortcode to your current theme's functions.php file:

function func_child_post_exists( $atts ){
	global $post;
		return toolset_get_related_post( $post->ID, 'state-city','child');
}
add_shortcode( 'child-post-exists', 'func_child_post_exists' );

Where:
- Replace 'state-city' with your post -relationship slug.

Now, call the function as given under:

[wpv-conditional if="( '[child-post-exists]' eq '0' )"]
 no child post available
[/wpv-conditional]


[wpv-conditional if="( '[child-post-exists]' ne '0' )"]
 Yes - yes child post available
[/wpv-conditional]

Where:
- Register shortcode name child-post-exists at :
=> Tooset => Settings => Front-end Content => Third-party shortcode arguments

Now, regarding your another query, do you want to check that post author is same as loggedin user?

#921664

Pat

Hi Minesh,

Thanks for your support.
I have tried your solution and the result is a blank page !
Even if I place only the shortcode (without conditional) !

For info, I have replaced 'state-city' with the slug of the postype that is child of the "objet" that is displayed in this page (content template of objet) which is 'wishlist'

Concerning your last question, yes I want to check also that post author is same as loggedin user

Regards
Pat

#921665

Minesh
Supporter

Languages: English (English )

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

Ok - great, Well - you do not need to replace post type name but post relationship slug.

Could you please share problem URL and access details so I can check whats going wrong there.

*** 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.

#921667

Pat

Sorry Minesh,

I dont understand what you mean by post relationship?
Could you give an example?
Thanks
Pat

#921669

Minesh
Supporter

Languages: English (English )

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

Go to Toolset => Relationships and check which is your parent/child relationship edit it - copy the slug and replace it within the code.

#921670

Pat

Hi Minesh,
Perhaps I'm not completely awake, but I cant find relationships under Toolset menu?
Regards
Pat

#921671

Minesh
Supporter

Languages: English (English )

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

I do not know what plugin version you are using now.

Are you using latest Toolset plugins?

Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#921673

Pat

Minesh,

You're right, I'm not using the latest version because this is on this site that I have made some modification on the Cred plugin in order to integrate the image rotation function.

Is there another way to define the relationship I need to place in the function?

Regards
Pat

#921677

Minesh
Supporter

Languages: English (English )

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

First of all could you send me debug info so I can check your current site setup and after that I can able to guide you. I think you missed to shared debug information.

Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#921679

Pat

Hi Minesh,

Here is the needed info.
Regards
Pat

#921691

Minesh
Supporter

Languages: English (English )

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

Add following shortcode to your current theme's functions.php file:

function func_child_post_exists( $atts ){
    global $post;

    $args = array(
                'post_type'             =>       'wishliste',    // change your child post slug IF needed
                'post_status'           =>       'publish',
                'posts_per_page'    =>       -1,
                'meta_query'            =>       array(
                            array(
                                    'key'           =>       '_wpcf_belongs_objet_id',
                                    'value'     =>       $post->ID,
                            )
                )
        );
        $chids = get_posts( $args );
        return count($chids);
 }
add_shortcode( 'child-post-exists', 'func_child_post_exists' );

Now, call the function as given under:

[wpv-conditional if="( '[child-post-exists]' eq '0' )"]
 no child post available
[/wpv-conditional]
 
 
[wpv-conditional if="( '[child-post-exists]' ne '0' )"]
 Yes - yes child post available
[/wpv-conditional]

Where:
- Register shortcode name child-post-exists at :
=> Tooset => Settings => Front-end Content => Third-party shortcode arguments

Could you please confirm it works or not. Also, you want to match the current post (which is a parent post you are displaying) author should be equal to loggedin user?

#921693

Pat

Hi Minesh,

That's perfect.
Yes I want to add another condition : that the author = the loggedin user.
Regards
Pat

#921709

Pat

Hi Minesh,

I think the best would be to modify the function "func_child_post_exists" in order to add the loggedin user condition.
That would allow to add this condition without changing anything to the content template.
Regards
Pat

#921710

Minesh
Supporter

Languages: English (English )

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

Ok - I've changed the code as per your request as given under:

function func_child_post_exists( $atts ){
    global $post;
    global $current_user; 
 
    $args = array(
                'post_type'             =>       'wishliste',    // change your child post slug IF needed
                'post_status'           =>       'publish',
                'posts_per_page'    =>       -1,
                'author'        =>  $current_user->ID, 
                'meta_query'            =>       array(
                            array(
                                    'key'           =>       '_wpcf_belongs_objet_id',
                                    'value'     =>       $post->ID,
                            )
                )
        );
        $chids = get_posts( $args );
        return count($chids);
 }
add_shortcode( 'child-post-exists', 'func_child_post_exists' );

I hope this is resolved now 🙂