Get value from several ACF relationship fields in one function

WordPress
15 October, 2021 00:34:48
dgedgw
Topics: 1
Messages: 1
Hi,
I've found very interesting solutions to display the value of ACF relationship fields in my PDF template
https://e2pdf.com/support/desk/get-relationship-value-from-acf-relationship-field
https://e2pdf.com/support/desk/get-value-from-another-element

It works, however, I would like to know if there is a way to do this for several ACF fields at the same time in one function. For the moment, I've written a new function + add_filter in my functions.php file for each field I need (see screenshot below).

Thanks for your help!
Kind regards,
Geraldine
15 October, 2021 05:15:22
E2Pdf
Support
Topics: 7
Messages: 3335
Hi,

Here is updated codes: https://codeshare.io/r9OgnY, https://codeshare.io/EB6oO8 with "elseif" conditions.

However currently you must be able to pull the data of related Post IDs and dynamic User data without additional hooks if you are able to get Post IDs / User IDs. Unfortunately it's not yet documented due the continued tests.

Here is the explanation with examples for getting related post data. It's need to add id="dynamic", close shortcode and put inside other shortcode which will get Post ID:

[e2pdf-wp id="dynamic" key="post_title"][e2pdf-wp key="custom_posts" meta="true"][/e2pdf-wp] - It will get post title for Post ID which is rendered by inner shortcode [e2pdf-wp key="custom_posts" meta="true"] (You must get exactly the Post ID).

If you have field where you can assign multiple releated Post IDs, then it's saved as array and you need to use "path" attribute to navigate through this array to get only 1 needed Post ID and the construction will look like this:

[e2pdf-wp id="dynamic" key="post_title"][e2pdf-wp key="custom_posts" meta="true" path="0"][/e2pdf-wp] - it will get post title for 1st Related Post ID.

[e2pdf-wp id="dynamic" key="post_title"][e2pdf-wp key="custom_posts" meta="true" path="1"][/e2pdf-wp] - it will get post title for 2nd added Related Post ID.

and so on...

* [e2pdf-wp id="dynamic" key="post_title"] - can be replaced with any other shortcode related to "e2pdf-wp" like getting meta value.

==============================

For user it will be same logic but with different "e2pdf-user" shortcode:

[e2pdf-user id="dynamic" key="user_nicename"][e2pdf-wp key="custom_user" meta="true"][/e2pdf-user]

* [e2pdf-wp key="custom_user" meta="true"] - must be rendered in User ID for which you need to get data.
** [e2pdf-user id="dynamic" key="user_nicename"] - can be replaced with any other shortcode related to "e2pdf-user" like getting meta value.

================================

P.S. The only limitation of using such shortcodes that if you will need to use multiple "e2pdf-wp" / "e2pdf-user" shortcode in one element, all they must be closed as:

[e2pdf-wp id="dynamic" key="post_title"][e2pdf-wp key="custom_posts" meta="true" path="0"][/e2pdf-wp]: [e2pdf-wp key="post_title"] - INCORRECT.

[e2pdf-wp id="dynamic" key="post_title"][e2pdf-wp key="custom_posts" meta="true" path="0"][/e2pdf-wp]: [e2pdf-wp key="post_title"][/e2pdf-wp] - CORRECT

P.P.S. E2Pdf also can output ACF data with shortcodes as [acf field="field_key" post_id="[id]"] but ONLY if the shortcode will output "string" (not array or post object) so it must be used carefully.

If you will have problem with setup above solution, you can attach some screenshots with your ACF field settings (key and return format) and data which you want to pull from that field so we can prepare some more examples.

We remain at your service.
We would really appreciate your feedback at WordPress.org!