WordPress: Get ID of custom post type within nested shortcodes

I’m currently having trouble getting the ID of a custom post type from within a nested shortcode. I’m not sure if this is possible.

Further information to describe my scenario:

  • I’ve created an “Activity” custom post type
  • I’ve created a shortcode called “activity” which displays the
    contents of an activity custom post type based on its ID, this can be
    set using the shortcode attributes
  • I’ve created a shortcode called “textarea” which displays a textarea
    within an acitivty custom post type

The issue that I’m having is that I’m trying to get the “Activity ID” from within the Textarea shortcode but I can’t figure a way of doing this.

Please see a diagram via this link to better describe my situation:


Activity Shortcode Code:

function display_activity($atts, $content = null){

        $a = shortcode_atts( array(
            'id' => null,
        ), $atts ) ;

        if (!is_null($a['id'])){

            $activityId = intval($a['id']);

            if (is_int($activityId) && $activityId > 0){

                $activityObj = get_post($activityId);
                $theTitle = sanitize_post_field( 'post_title', $activityObj->post_title, $activityId, 'display' );
                $theContent = apply_filters('the_content', $activityObj->post_content);

                $html = "<div id='sbusuws-activity-" . $activityId . "' class='sbusuws-activity'>";

                $html .= "<h3 class='sbusuws-activity-title'>" . $theTitle . "</h3>";

                $html .= "<div class='sbusuws-activity-content'>";
                $html .= do_shortcode($theContent);
                $html .= "</div>";

                $html .= "</div>";

                return $html;




Textarea Shortcode:

function display_textarea($atts){

$activityId = ""; // <--- This is the problem
    $textareaId = $activityId . "-sbusuws-activity-textarea";
    $html = "<textarea id='" . $textareaId . "' rows='5' cols='20'>";

    return $html;


The idea was to make the textarea shortcode as simple as possible, ie: [textarea]

I’d appreciate any help with this issue.


Source: shortcode

Leave a Reply

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