1

I have 2 custom post types, project and person. Both have several custom fields. I'm working on a template for the project custom post type.

In the project custom fields I have several fields of type relation, as seen in this screenshot. Unfortunately I don't really undestand what they mean and how to use them, but that is not my main question.enter image description here

Next, inside the person custom fields, I have only text fields, like first_name, last_name, etc.

Now let's say I have one custom post type of type person. That person has contributed to a project, which is a custom post type of type project.

How can I now, when working on the template of the project posts, include an [acf field] shortcode to a custom field of type person?

Here is a screenshot/concept of what I want to do: Circled in red are information pulled from the project custom fields, whereas circled in blue are information pulled from the person custom fields. enter image description here

Notes:

  • I'm fairly new to wordpress and have no knowledge in php

  • Ideally a solution to my problem should not need me to add code to php files as that is not desired by my client. Preferably it should be doable inside the WPBakery template editor.

    • Installed and usable plugins are ACF, WPBakery, toolset, salient, acf-vc, custom post type UI

Thank you for your help, I hope I succeeded to phrase my problem in an understandable manner.

UPDATE

I found this plugin, which seems to do provide one solution: nested shortcodes: https://wordpress.org/plugins/nested-shortcodes/

Unfortunately, it also poses constraints, and has not allowed me to find a solution. What I want to do is this:

Input: [acf field="first_name" post_id=[acf field="student"]]

With the expected output being: John Doe

Unfortunately the output I get is: ]

Which according to them is partly due to shortcode limitations listed here https://codex.wordpress.org/Shortcode_API#Limitations

On the other hand, this hardcoded workaround works:

Input: [acf field="student"] Output: 89

Input: [acf field="first_name" post_id=89] Output: John Doe

Unfortunately this workaround has the hardcoded unique ID 89, which makes it thus unable to use as a template for future usage.

Does anyone have a solution or better workaround that does not necessite me to hardcode? I'm also open for completely new ideas and directions, as long as it helps me solve this problem

0

I think that, as you are using a Relationship model in your ACF settings, you have to iterate through the elements on your page. For that reason, you can simply use the code from the ACF docs, and add it to your single.php or page.php. https://www.advancedcustomfields.com/resources/relationship/

If you still want to use shortcodes, you can create an own shortcode including the sample code from the link above.

Hope this helps!

0

Here is a solution I found with the help of be.fresh. Adding this line of code in the functions.php file creates a shortcode [person], which gives the correct output.

function person_func() {
    $postID = do_shortcode("[acf field='student']");
    $postField = get_field("first_name", $postID);
    return $postField;
}
add_shortcode( 'person', 'person_func' );

Why does it work? I found here that ACF accepts also post ID's in their get_field() function, so I can circumvent my problem with the hardcoded 89 as post ID.

Of course this function can be made more useful by creating a shortcode that can take attributes, as can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.