Skip Navigation

[Resolved] Conditional check of a specific parent post

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by katerinaA 1 year, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2607553

hello, i nedd some syntax help with a condition. I use Custom Post Types, Custom taxonomies and Relationship. I have written the following:

[wpv-conditional if="( '[types termmeta='ab' output='raw'][/types]' ne '') AND ( '[wpv-post-id item="@e-shop.parent"]' eq '' )"]show content[/wpv-conditional]

it works, but i need it to be more specific. I would like it to check a specific @e-shop.parent and show the content only if this parent is connected. I guess the identification can work through the post parent ID
Thank you

#2607777

Hi,

Thank you for contacting us and I'd be happy to assist.

If you'd like to check if a specific post is connected as a parent relationship item, you can change the conditional statement to use its ID like this:


[wpv-conditional if="( '[types termmeta='ab' output='raw'][/types]' ne '') AND ( '[wpv-post-id item="@e-shop.parent"]' eq '12345' )"]
show content
[/wpv-conditional]

Note: You'll replace '12345' is the ID of the target parent post that you'd like to check for.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2607783

Hi Waqar, Thank you for your prompt answer.

It still not works as I was expecting.
Probably it is a concept issue!
So what i need is to "show content" when the following parameters are met:

Check if [types termmeta='ab' output='raw'][/types] has any content but only "show content" if [wpv-post-id item="@e-shop.parent"] is NOT connected to a parent with post ID '1234'.

NOTE: My relationship is Many-to-many. Could it be that the syntax ".parent" is not correct?

Thank you again for the help

#2608195

Thank you for sharing these details.

The 'item' attribute method ( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/ ), can only return one related post. This is why it is not effective for checking the existence of any particular related post when multiple related posts can exist.

For a conditional check like this, you'll need a custom shortcode, that can get all the related parent posts, using the 'toolset_get_related_posts' function, and then check if the target post exists in those posts or not.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts)

For example:


add_shortcode( 'check-for-parent', 'check_for_parent_func');
function check_for_parent_func($atts) {

	// target post's ID from shortcode attribute 'target'
	$target_ID = $atts['target'];
	
	// current post's ID
	$postid = get_the_ID();

	// setting default return value to '0'
	$return_value = 0;

	// get related parent posts from the current post
	$get_results = toolset_get_related_posts( $postid, 'e-shop', 'child', 9999, 0, array(), 'post_id', 'parent' );

	// if some parent posts are found
	if(!empty($get_results)) {
		// if the target post is among the found parent posts 
		if(in_array($target_ID, $get_results)) {
			// change return value to 1
			$return_value = 1;
		}
	}

	// return the final return value
	return $return_value;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "check-for-parent" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This custom shortcode accepts one parameter 'target' which should hold the ID of the target parent post to look for:


[check-for-parent target='1234']

If the current post has this target post connected as the parent post, it will return '1' and if not, it will return '0'. This will allow you to easily construct your conditional statements, as needed.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2608481

Waqar I appreciate your help and accuracy! Thank you very much. I would have never solved this one on my own!
Million thanx again!