Aleš Sýkora wrote

Get a grandparent post ID from a shortcode

1. 7. 2019 in

Sometimes you want to access a grandparent post ID in the context of a child post. The Toolset method involves nested Content Templates or Views. This simple shortcode will allow you to get that grandparent post ID without the need for additional Content Templates or Views, so it’s useful in conditionals. Add the following code to a new snippet in Toolset > Settings > Custom Code, or add it to your child theme’s functions.php file:

function get_grandparent_id_func( $atts )
{
  $a = shortcode_atts( array(
      'postid' => 0,
      'parentrel' => '',
      'grandparentrel' => ''
  ), $atts );

  $parent = toolset_get_related_post( $a['postid'], $a['parentrel']);
  $grandparent = toolset_get_related_post( $parent, $a['grandparentrel']);

  return $grandparent;
}
add_shortcode( 'get-grandparent-id', 'get_grandparent_id_func' );

To use the shortcode you’ll pass in 3 parameters: the child post ID, the parent relationship slug, and the grandparent relationship slug.

Grandparent post ID: [get-grandparent-id postid="[wpv-post-id]" parentrel="your-parent-relationship-slug" grandparentrel="your-grandparent-relationship-slug"]

Snippet created by: Beda Schmid

for Toolset, Toolset Types, Toolset Views

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Oxygen’s Number Counter tricks – Dynamic data, speed and more
14. 7. 2021 in ,
All tips should be working for both Number Counter composite element and Circle Number Counter composite element in Oxygen Builder for WordPress. Using Dynamic Data in Number Counter Using the Number Counter from Oxygen Builder’s composite elements is cool until you need to use the dynamic data. When you add dynamic data source, span is […]
Read More
Aleš Sýkora wrote
Translate Phone number error message in Fluent Forms
15. 4. 2021 in
If you want to translate the error message, when user fill in wrong phone number in fluent forms, you can use this custom snippet:
Read More
Aleš Sýkora wrote
Disable Astra Theme Next/previous Post link
11. 7. 2019 in
Disable Next/Previous Post in Astra Theme.
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram