Skip Navigation

[Resolved] I need to show the Title of a Post Reference Type field using PHP

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

Problem: I would like to display the title of the post selected in a post reference field using PHP, but when I output the value of types_render_field I get a number.

Solution: This number is the referenced post ID. You can use it to get the post title with the WordPress function get_the_title:

$tarif = types_render_field( "tarif" ); 
$title = get_the_title($tarif); 
echo ucwords ($title);

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://developer.wordpress.org/reference/functions/get_the_title/

This support ticket is created 3 years, 5 months 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)

Author
Posts
#2077889

I want to display the title of a Post Reference Type field using PHP, but I only get a number returned. I assume that is the Post ID. Right now, I use:

<tr>
<td>Tarif</td>
<td><?php $tarif = types_render_field( "tarif" ); echo ucwords ($tarif); ?></td>
</tr>

#2078449

Hi, yes rendering the post reference field with the Types Field API gives you the referenced post's ID. You should be able to use that post ID to get the post title in PHP and display it in your code example like so:

<tr>
<td>Tarif</td>
<td><?php $tarif = types_render_field( "tarif" ); $title = get_the_title($tarif); echo ucwords ($title); ?></td>
</tr>

WordPress documentation for get_the_title:
https://developer.wordpress.org/reference/functions/get_the_title/

Let me know if I've misunderstood what you want to achieve, or if this is not working as expected.

#2078503

My issue is resolved now. Thank you!