• Resolved Mark Smallman

    (@marks99)


    Is it possible to pass the post_ID ‘s of each CPT that is included in a QueryLoop – For use in a containing element?
    Basically, the post ID needs to be passed to a shortcode (button) that needs it to check if a user is ‘following’ each specific post.
    The shortcode is part of a custom plugin that we have working in other sections of the site, apart from the home page- where we use the QueryLoop to bring in elements of a CPT posts. We need the postID to add it to a data attribute. EG. data-post-id=”XXXX” .
    Hope I have explained this well enough.
    Mark

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support David

    (@diggeddy)

    Hi there,

    you could use the render_block hook to execute your shortcode inside the loop where you can get_the_id()

    1. Add a Headline Block inside your Query Loop Post Template block.

    2. Give the Headline a CSS Class eg. my-custom-css-class
    Note that class is stored in the $block_class in the snippet below.

    3. Add this PHP snippet and update the $short_code to match your needs.

    add_filter( 'render_block', function( $block_content, $block ) {
        // add class to target block
        $block_class = 'my-custom-css-class';
        if ( 
            ! empty( $block['attrs']['className'] ) && 
            $block_class === $block['attrs']['className'] 
        ) {
            // get the ID
            $post_id = get_the_ID();
            // build and return  my shortcode
            $short_code = '[my_shortcode data-post-id="' . $post_id . '"]';
            return do_shortcode($short_code );  
        }
    
        return $block_content;
    }, 10, 2 );

    Thread Starter Mark Smallman

    (@marks99)

    Thanks @diggeddy worked Perfect!
    I was able to work this into the (custom) plugin that creates the original shortcode, without having to change a load of other parts of the site.

    Cheers, Mark

    Plugin Support David

    (@diggeddy)

    Awesome, glad to hear that!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get postid of posts included in Query Loop’ is closed to new replies.