Skip Navigation

[Resolved] Getting the current post id in a SSR block placed in a View Loop

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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 9 replies, has 2 voices.

Last updated by jasonM-21 3 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2376697

I need to be able to call something like get_the_ID() within a SSR block that's placed in a view loop. I've tried
do_shortcode('[wpv-post-id]');

but it just returns the id of the parent post. How can I get the ID of each result in the loop?

#2376789

Hi,

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

The shortcode "[wpv-post-id]" should be returning the current post's ID in the view's loop.

I'm not aware of the SSR block, so can you please share a temporary admin login details of a website where this block can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

regards,
Waqar

#2376817

A Server Side Render block. One that is rendered with PHP. I need to do some processing on the data and need to be able to get the post id in each iteration of View Loop.

Is there some PHP function I can call to get the current post id?

#2377033

Thanks for writing back.

Here is a custom shortcode that I used inside the view's loop, to confirm that both the 'get_the_ID' and 'do_shortcode('[wpv-post-id]')' functions, returns current post ID from the view loop and not the current page's ID:


add_shortcode('get_current_id', 'get_current_id_func');
function get_current_id_func() {
	
	//$ID = get_the_ID();
	$ID = do_shortcode('[wpv-post-id]');

	return $ID;
}

If you could share some background information about what you're planning to achieve using this custom block, I'll see if the developers could share some pointers or suggestions, to sort this out.

#2377439

Here's the render callback function for my block:

<?php

add_shortcode('get_current_id', 'get_current_id_func');
function get_current_id_func() {

  //$ID = get_the_ID();
  $ID = do_shortcode('[wpv-post-id]');

  return $ID;
}


function example_block_render_callback($attributes, $content) {

  $out = "<ul>";
  $out .= "<li>" . get_the_ID() . "</li>";
  $out .= "<li>" . do_shortcode('[get_current_id]') . "</li>";
  $out .= "<li>" . do_shortcode('[wpv-post-id]') . "</li>";
  $out .= "</ul>";

  return $out;
}

The only thing getting output is the host page id. I know that you can use the shortcode block to output the page id on the page, but that isn't what I need. I need to be able to work with the post id for the items in the View in PHP.

#2379307

Would it be possible for your to share a clone/snapshot of a test website where this custom block can be seen in action?
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

This will allow us to troubleshoot this and suggest the next steps, accordingly.

Note: I've set your next reply as private.

#2379899

I'm not really able to give access to the site, but I have an example plugin that shows what I'm talking about:

hidden link

Basically, it just outputs the post ID when the block is placed. If you place it inside the View Loop I would like it to be the idea of the post that loop is displaying, but instead it's the id of the page the block is on.

#2380311

Thank you for sharing the plugin and in my tests, I was able to reproduce this behavior too.

I've shared these findings with the concerned team for further insights and will keep you updated through this ticket.

#2381417

Thank you for waiting and I have a workaround to share that worked in my tests.

1. Create a new content template, but don't assign it to any page/post.

2. Inside your view's loop, you can include this new content template, which will help in changing the current post's scope to the current post in the loop.

3. When you'll insert your "Test Block" inside this content template, you'll see it will correctly return the current post's ID and not the current page's ID.

#2384253

My issue is resolved now. Thank you!