Use block content to get post ID in WP_Query loop

111 Views Asked by At

I have made a plugin to save Testimonials. For this:

  • I have a custom post type called Testimonials, under which Testimonials are saved.
  • I have a custom block, which the user can put anywhere in the editor and a shortcode will be printed there. The user can specify which Testimonial to use by putting the ID of the Testimonial in the block editor.
  • I have a custom shortcode, which will make sure it all looks on the page how it should be.
  • On the page, a div with the output is shown; the div has the ID which is put in by the user in the editor.

Within the editor, in the custom block, the user can specify the ID of the Testimonial to show. Now, I want to use that ID within a WP_Query loop, so I can make sure that it will only produce the specified Testimonial.

I have been trying to get the div ID into a PHP variable. And I have been trying to get some Javascript-action to magically turn the ID into something with which I can make a PHP variable. Unfortunately, I am apparantly not proficient enough in either.

So, what I am simply trying to do is this:

<div id="123">
<?php $testimonial_ID = /* code to get parent div-ID into this variable, the ID is 123 */ ?>
<?php $test = new WP_query(array('post_type'=>'testimonials','posts_per_page'=>1,'p'=$testimonial_ID)); ?>

...

</div>
0

There are 0 best solutions below