Skip Navigation

[Resuelto] How to access the data stored for a entry in a post reference field using PHP?

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I have a post reference custom field and I would like to access the reference post's title and ID in PHP.

Solution:
Use get_post_meta to determine the reference post ID, then use get_the_title to determine the reference post title:

$contributor_id2 = get_post_meta( $post_id, "wpcf-post-contributor2", true);
$contributor_id2_title = get_the_title( $contributor_id2 );
echo "Post ID: " . $contributor_id2 . ", Title: " . $contributor_id2_title; 
This support ticket is created hace 5 años, 10 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 respuestas, has 2 mensajes.

Last updated by Christian Cox hace 5 años, 10 meses.

Assisted by: Christian Cox.

Autor
Mensajes
#915277

I've created a new post reference field and I'd like to use PHP to display the title and ID of the selected post.

I've tried the standard

<?php $contributor_id2 = types_render_field("post-contributor2", array('output'=> 'normal'));?>

hoping that I'd get the ID of the selected post - but don't seem to be pulling any data at all with that function.

News Post Reference field types is not listed in API documentation yet

#915501

Hi, you can use get_post_meta in PHP to retrieve the related post's ID, then use get_the_title() to access the related post's title:

$contributor_id2 = get_post_meta( $post_id, "wpcf-post-contributor2", true);
$contributor_id2_title = get_the_title( $contributor_id2 );
echo "Post ID: " . $contributor_id2 . ", Title: " . $contributor_id2_title; 

Note that the custom field slug in get_post_meta should include the 'wpcf-' prefix.

#915740

Thanks Christian - just what I needed. Do you think you'll be creating a types_render_field() function in the future to handle this new field type?

#915801

Probably not, because the post reference custom field simply stores a post ID. You can use types_render_field to display custom field information from that related post, like if there was a select custom field and you wanted to show the selected value. The types_render_field function is not designed to display basic post information like post title, because that's not stored in the custom field. That information can be determined more efficiently using the raw post ID to query the posts table.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.