Skip Navigation

[Resolved] Display contents of a custom field based on the contents of another custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two custom post types in my site. They are not related using Toolset Post Relationships, but they are related using a post ID field. Custom Post Type A (CPT A) contains a custom date field. Custom Post Type B (CPT B) contains a custom number field that stores the numeric ID of some post from CPT A. In the template for CPT B, I would like to display the date field from the post in CPT A with the ID stored in CPT B's numeric field. The following solution works well technically, but uses a hard-coded post ID:

[types field='person-date-of-birth' item='26'][/types]

The date of birth stored in post ID 26 is displayed correctly, but it's not practical for me to use a hard-coded post ID. So I tried using a nested shortcode in the "item" attribute of the outer shortcode, but it is not working correctly:

[types field='schema-person-monastic-name' item='[types field='schema-person-honorific-prefix'][/types]'][/types]

Solution: Any time you nest Types field shortcodes like this, you should use the raw output attribute in the inner nested shortcode. This way no extra spaces, line breaks, or HTML formatting are included in the output, which would break the outer shortcode. It is also recommended to alternate between single and double quotes per nesting level, like this:

[types field="schema-person-monastic-name" item="[types field='schema-person-honorific-prefix' output='raw'][/types]"][/types]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#numeric

This support ticket is created 5 years, 1 month 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by AtefR7377 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1389857

Hi,
I have a custom post type called: "Persons" and another called "Books". these 2 CPTs are NOT related with Toolset Relationships (e.g. one to many or many to many).

The persons CPT is for posts about book authors.

In the persons CPT posts I have multiple custom fields for the author biography, e.g. "date of birth", "occupation",,,etc

I want to display the author biography information in the books posts.

Currently I keep copying the data manually, but I want to utilize toolset functionality.

I can successfully display the content of of a field based on a person's post ID, like this:
`[types field='person-date-of-birth' item='26'][/types]` where `26` is the post ID of the author.

However, my posts have layouts and I can't manually keep changing the item ID for every Book post.

I thought of creating a custom field in the Books CPT and manually type the relevant post ID, and use a nested Toolset code to fetch the post ID, like this:

`[types field='schema-person-monastic-name' item='[types field='schema-person-honorific-prefix'][/types]'][/types]`

but this did not work.
I also tried the following. None of them worked:

`[types field='schema-person-monastic-name' item='schema-person-honorific-prefix'][/types]`
`[types field='schema-person-monastic-name' item='wpcf-schema-person-honorific-prefix'][/types]`

So how can I display the content of a custom field of another post, by specifying the other post ID using a custom field in the destination post?

thanks a lot.

#1390513

Hello, have you tried applying the "raw" output option in the schema-person-honorific-prefix field? This will use the value directly from the database without any additional markup or formatting. I recommend using the raw output any time you want to use a field as the value of another attribute, and to alternate single and double quotes when nesting fields, like this:

[types field="schema-person-monastic-name" item="[types field='schema-person-honorific-prefix' output='raw'][/types]"][/types]

If that doesn't work as expected, I'll need to take a closer look.

#1390793

Hi Christian,
Thank you very much. you are a champion, as always, you helped me in this instance too.

I just need your help in one last step, which is showing the information correctly.

I am using these fields to populate the (Schema.org) code for books microdata, so I had to "hardwire" the code into my template.

Currently, my code is like this:

	$author_job_title = array_filter( array (
						'jobTitle' => types_render_field( "schema-author-job-title", array () ),
						) );
					if ( ! empty( $author_job_title ) ) {
							echo '<tr><td>author's Job title</td><td><span itemprop="author" itemscope itemtype="<em><u>hidden link</u></em>">';
						
						foreach ( $author_job_title as $prop => $value ) {
							printf( '<span itemprop="%s"> %s </span>', $prop, $value );
						}
						echo '</span></td></tr>';
					}

This works perfectly when I manually re-enter the author's data in the book post. But now that you solved my issue,

So I will create a custom field, in which i will manually add the author's post ID in it, let's call it "author-post-id".

I need your help in modifying the Toolset "render" function above, to say:

If the "schema-author-job-title" field of the post which its id is written in the custom field "author-post-id" is not empty, then render the "schema-author-job-title" field from post with ID stored in "author-post-id" field.

So basically how to call up the reference post in this function:

'jobTitle' => types_render_field( "schema-author-job-title", array () ),

I really appreciate your help.

Thanks a lot.

#1390845

Hi Christian,

Please note that in my previous reply, I had an extra apostrophe in the words "Author's Job title". this is just a typo. My actual code does not have this, otherwise it will break the code.

Anyway, based on this support thread,:
https://toolset.com/forums/topic/types_render_field-with-a-specific-post-id/

I tried the following code, but had to switch the double quote to a single quote mark for the wordpress plugin editor to save. otherwise it till not save.
however, nothing is showing when I m using this code, so I believe that I am doing something wrong:

$author_job_title = array_filter( array (
						'jobTitle' => types_render_field( '[types field="schema-author-job-title" item="[types field="author-post-id" output="raw"][/types]]"[/types]', array () ),
						) );
					if ( ! empty( $author_job_title ) ) {
							echo '<tr><td>Author Job Title</td><td><span itemprop="author" itemscope itemtype="<em><u>hidden link</u></em>">';
						
						foreach ( $author_job_title as $prop => $value ) {
							printf( '<span itemprop="%s"> %s </span>', $prop, $value );
						}
						echo '</span></td></tr>';
					}
#1391287

You cannot use Types field shortcodes directly in PHP. You should always use types_render_field or get_post_meta to get the value of a custom field. Here's an example using types_render_field to get the value of a field in the author post if the author post ID field is set:

global $post;
$post_id = $post->ID; // or change this to some other post ID
$author_id = types_render_field("author-post-id", array( "item" => $post_id, "output" => "raw" ) );
if( $author_id ) {
  $author_job_title = types_render_field( "schema-author-job-title", array( "item" => $author_id) );
}
#1391393

Ok thanks a lot Christian,
I modified the code as follows. It did not give any errors when saving, but no information was displayed.
The function displays the words "Author Job Title", but the job title itself does not display.

global $post;
						$post_id = $post->ID; // or change this to some other post ID
						$author_id = types_render_field("author-post-id", array( "item" => $post_id, "output" => "raw" ) );
						if( $author_id ) {
  							$author_job_title = types_render_field( "schema-author-job-title", array( "item" => $author_id) );
							}
			
					if ( ! empty( $author_job_title ) ) {
							echo '<tr><td>Author Job Title</td><td><span itemprop="author" itemscope itemtype="<em><u>hidden link</u></em>">';
						
							foreach ( $author_job_title as $prop => $value ) {
							printf( '<span itemprop="%s"> %s </span>', $prop, $value );
						}
						
						echo '</span></td></tr>';
					}	

can you please help me close out this function, please?
thanks in advance.

#1391465

In my code example, $author_job_title is just a simple string. It's not an array, so this code isn't appropriate:

foreach ( $author_job_title as $prop => $value ) {
                            printf( '<span itemprop="%s"> %s </span>', $prop, $value );
                        }

I'm not sure what this code is supposed to do, but it should be refactored to work with a simple string instead of some array.

#1394485

My issue is resolved now. Thank you!