How to show feature image of ACF relationship field inside ACF repeater field

WordPress
12 January, 2023 02:52:48
natacha
Topics: 2
Messages: 8
Hi,

I have an ACF repeater field containing a relationship field and an image field.

I am able to display the image using an image template object with the value:
[acf field='artworks_0_custom_image_url' post_id="[id]"]

----

I am able to display the post content of the first relationship field item using a HTML template object with the value:
[e2pdf-wp key="artworks_0_artwork" meta="true" path="0"]

and the filter:

function e2pdf_extension_render_artwork_values( $value, $element_id, $template_id, $item, $dataset )
{
if ($value && $template_id == '1') {

if ($element_id == '10') { //HTML object
$related_post = get_post($value, ARRAY_A);
if ($related_post && isset($related_post['post_content'])) {
$output = $related_post['post_content'];
return $output;
} else {
return '';
}
}

}
return $value;
}
add_filter('e2pdf_extension_render_shortcodes_value', 'e2pdf_extension_render_artwork_values', 10, 5);

----

I am having troubles showing the feature image of the relationship field item.

I have tried using an image template object with the value:
[e2pdf-wp key="artworks_0_artwork" meta="true" path="0"]

and the filter:

function e2pdf_extension_render_artwork_values( $value, $element_id, $template_id, $item, $dataset )
{
if ($value && $template_id == '1') {

if ($element_id == '8') { //image object
$related_post = get_post($value, ARRAY_A);
if ($related_post && get_the_post_thumbnail($related_post['ID']) ) {
$output = get_the_post_thumbnail_url($related_post['ID'], 'full');
return $output;
} else {
return '';
}
}

}
return $value;
}
add_filter('e2pdf_extension_render_shortcodes_value', 'e2pdf_extension_render_artwork_values', 10, 5);

The above does not work.

I have also tried using this value directly in the image object:
[e2pdf-wp key="artworks_0_artwork" meta="true" path="0" attachment_image_url="true" size="full"]

Can you advise how I can show the feature image of the relationship field that is inside the ACF repeater object?

Thank you!

12 January, 2023 03:20:30
E2Pdf
Support
Topics: 7
Messages: 3188
Hi,

Can you please create 2 "textarea" inside E2Pdf Template and use separate shortcodes:

[acf field="artworks_0_artwork" post_id="[id]"]

[e2pdf-wp key="artworks_0_artwork" meta="true"]

Is any of these shortcodes output just the related Post ID?

We would really appreciate your feedback at WordPress.org!
12 January, 2023 03:52:54
natacha
Topics: 2
Messages: 8
This one: [acf field='artworks_0_artwork' post_id="[id]"]

generates the error:

Fatal error: Uncaught Error: Object of class WP_Post could not be converted to string in \wp-content\plugins\advanced-custom-fields-pro\includes\api\api-template.php on line 902

I assume because the returning value is not a String.

This one: [e2pdf-wp key="artworks_0_artwork" meta="true"]

displays in the textarea: a:1:{i:0;s:5:"26828";}

----

The above got me thinking - Something else I tried...

Using this:
[e2pdf-wp key="artworks_0_artwork" meta="true" path="0"]
as the value in an image object displays the correct relationship field ID as the image.

But if I use a filter to try and reference this ID value:

function e2pdf_extension_render_artwork_values( $value, $element_id, $template_id, $item, $dataset ) {

if ($value && $template_id == '1' && $element_id == '8') { //Artwork image

$related_post_id = $value;

write_log('related_post_id = '.$related_post_id);

if (get_the_post_thumbnail($related_post_id)) {
$image_url = get_the_post_thumbnail_url($related_post_id, 'full');
write_log($image_url);
return $image_url;
} else {
return '';
}
}
return $value;
}
add_filter('e2pdf_extension_render_shortcodes_value', 'e2pdf_extension_render_artwork_values', 10, 5);

My log file shows the $value as:

related_post_id = iVBORw0KGgoAAAANSUhEUgAABz0AAACzCAYAAAAT4wHC .... etc

I assume it is bringing in the image data as the $value.

----

Any other clues on how I can get access to the ID in the filter and get this image object to show the feature image?

12 January, 2023 04:02:17
E2Pdf
Support
Topics: 7
Messages: 3188
Try please below shortcode inside "textarea", without any PHP filters:

[e2pdf-wp id="dynamic" key="get_the_post_thumbnail_url" size="thumbnail"][e2pdf-wp key="artworks_0_artwork" meta="true" path="0"][/e2pdf-wp]

It must output the URL to Post Thumbnail. If it will work, just move this shortcode to the "Image" object and remove any PHP filters added to the "Image" object.
We would really appreciate your feedback at WordPress.org!
12 January, 2023 04:06:57
E2Pdf
Support
Topics: 7
Messages: 3188
If it will work, you can also replace the shortcode for the Post Content and avoid using PHP filters:

[e2pdf-wp id="dynamic" key="post_content"][e2pdf-wp key="artworks_0_artwork" meta="true" path="0"][/e2pdf-wp]

P.S. Keep in mind please that if in the same "html" / "field", where such shortcode will be placed, will have any other [e2pdf-wp] shortcodes, it must be closed as [e2pdf-wp key="post_content"][/e2pdf-wp].
We would really appreciate your feedback at WordPress.org!
12 January, 2023 04:29:38
natacha
Topics: 2
Messages: 8
That worked!

BUT can you confirm if using dynamic shortcodes like this prevents the filters from working?

Since now my post_content filter no longer works. The reason I am using a filter on the post_content is that I concatenate extra ACF group field values, and number formatting:

if ($element_id == '10') { //Artwork text description

//Show Artwork field post title field & price (if any)

$related_post = get_post($value, ARRAY_A);
if ($related_post) {

$image_desc = $related_post['post_content'];

$price_values = get_field('pricing_details', $related_post['ID']);
$retail_price = number_format($price_values['retail_price']); //add thousands comma separator

$output = ($retail_price) ? $image_desc . ', $' . $retail_price : $image_desc;
return $output;

} else {
return '';
}

}

Does this mean the above can no longer work due to the dynamic data shortcodes?
12 January, 2023 04:55:51
E2Pdf
Support
Topics: 7
Messages: 3188
You can, however, it must be used another filter, for shortcode directly: https://codeshare.io/8pdnXj

and your content shortcode must be modified to (added pricing_details="true"):

[e2pdf-wp id="dynamic" key="post_content" pricing_details="true"][e2pdf-wp key="artworks_0_artwork" meta="true" path="0"][/e2pdf-wp]
We would really appreciate your feedback at WordPress.org!
12 January, 2023 07:53:56
natacha
Topics: 2
Messages: 8
I can confirm that everything is working perfect! I learnt a lot of advanced E2PDF / advanced ACF field integrations from your code responses above.

I hope this helps others too. I learnt E2PDF by reading through other people's questions, hence this post should hopefully help others also!