Skip Navigation

[Resolved] Need to get 1 root parent

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 7.03 hours from now. Thank you for your understanding.

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)

Author
Posts
#2542453

Tell us what you are trying to do? I'm trying to get the top post of a hierarchy.

Is there any documentation that you are following? I used the Select top-level posts with no parent. But I now realize that it gets all top parents. I just need it for the current hierarchy.

I did create a view to crawl up the hierarchy, however, I need to include this view in another, and it seems it doesn't like three nested shortcodes.

[wpv-view name="get-course-content" ids="[wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]']" root="true" content="[wpv-post-field name='wpcf-course-content-type']"]

This is the one it breaks on [wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]']
On its own it works, but not when using in another view.

So, I figured, maybe create a container around it that just get the id from the current page viewed, that would work, but there seems to be no way to get that in a view either.

So, the solution I need, is to be able to nest this [wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]'] in [wpv-view name="get-course-content" ids="[wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]']" root="true" content="[wpv-post-field name='wpcf-course-content-type']"]

Or create a another view that gets the id of the current post, in that one I will use the other view, i.e. [wpv-view name='get-root-id-for-hierarchy-container']

So, it would be [wpv-view name="get-course-content" ids="[wpv-view name='get-root-id-for-hierarchy-container']" root="true" content="[wpv-post-field name='wpcf-course-content-type']"]
In other words, I'm getting around the fact the shortcode can't be nested three levels.

#2543655

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I am not sure if it is possible to do a three-level nesting.

If the issue is the correct " and' and it causes the issue, you can create a custom shortcode to add one nested one and then use a simpler shortcode form for the other nesting functionality.

https://toolset.com/documentation/programmer-reference/adding-custom-code/

You can register your custom shortcode in Toolset to be accepted as an attribute:

https://toolset.com/documentation/legacy-features/views-plugin/shortcodes-within-shortcodes/

See if that method helps.

#2543669

Hi and thanks for responding. What would I put in the custom code?
Do you mean the custom code would just get the post id? And if I register that, it allows that third level nesting?

So, it would become
[wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]'] in [wpv-view name="get-course-content" ids="[wpv-view name='get-root-id-for-hierarchy' ids='[MY-CUSTOM-CODE]']" root="true" content="[wpv-post-field name='wpcf-course-content-type']"]

#2543697

I think I might have the solution and register the shortcode.

#2543749

I was wrong. I registered [wpv-post-id] under Settings -> Third-party shortcode arguments
But it still barks at this
” root=”true” content=”course”]

Which is because of the nested ids='[wpv-post-id]' on the nested view.
Everything works on its own, i.e. if I pull out [wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]'] it returns as expected.

[wpv-view name="get-course-content" ids="[wpv-view name='get-root-id-for-hierarchy' ids='[wpv-post-id]']" root="true" content="[wpv-post-field name='wpcf-course-content-type']"]

#2544021

Hi,

Thanks for writing back.

If your goal is to get the top-level parent of the current post, it would be simpler to register a custom shortcode that uses the function "get_post_ancestors":
https://developer.wordpress.org/reference/functions/get_post_ancestors/

For example:


add_shortcode('custom_get_post_ancestors', 'custom_get_post_ancestors_func');
function custom_get_post_ancestors_func() {
	global $post;
	$parents = get_post_ancestors( $post );
	$id = $post->ID;
	/* Get the ID of the 'top most' post */
	if ( ! empty( $parents ) ) {
		$id = array_pop( $parents );
	}
	return $id;
}

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 "custom_get_post_ancestors" in the Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This shortcode will return the ID of the top-level parent post, with respect to the current post and if the current post is the top-level parent post then it will be the same as the current post's ID.

You can use that ID in the item attribute to show any information from that post, for example, to show its title, you can use it with the "wpv-post-title" shortcode, like this:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/ )


[wpv-post-title item="[custom_get_post_ancestors]"]

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

regards,
Waqar