Skip Navigation

[Resolved] Echo a image field of an associated post with PHP

This support ticket is created 5 years, 7 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#1252441
Image 2 - field to reference a condominium inside the listing register.png
Image 1 (featured image of condominium).png

Hello!

Using types, I've created a custom post type, called condominium(let's call it of A) and I have the post type listings (let's call if of B).

The point is that some listings, that are terrains, doesn't have photos itself, and in this case, I should retrieve an image of the condominium that this terrain is.

In the fields of the condominium, I have added a field to be the desired featured image of that condominium (See image 1, in attach).

In the listing fields, I have added a field to reference a condominium to that listing. (See image 2)
(I know that's also possible to do that with relationships, also, but when you create a new post, the relationship is just possible when you 'publish' or update it, what's a quite annoying for the user that has to fill all the fields of a big amount of listings.)

I have done my website hybrid with types/views and PHP, so my point is to have a PHP function that retrieves that featured image field of a condominium, inside the listing.

I saw with views that the desired shortcode to do this, in my structure is:
[types field='foto-em-destaque' size='full' item='@associar-proprietario.parent' url='true'][/types]

Is there any way to use PHP to set a variable with the value of the shortcode above?

#1252571

Hello,

I assume we are talking about this case:
Two post types:
- condominium
- listings
One-to-many relationship between post type "listings" and "condominium"
So each "listings" can connect with multiple "condominium" posts, you can follow our document to setup a post query, get related "condominium" posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

and display those "condominium" post information(image field "foto-em-destaque"):
https://toolset.com/documentation/customizing-sites-using-php/functions/

#1252843

Hey Luo!

Thanks for your reply.

Actually this relationship doesn't exists it's just a simple field reference, as the shortcode that I mentioned above.

In this case, the post relationship query won't work, since the condominium is not related to the listing, it's just a reference field. or it will?

#1252849

In your case, I suggest you setup the one-to-many relationship instead of reference field, so you will be able to query related "condominium" posts in a single listings post, see the documents I mentioned above.

#1261755

My issue is resolved now. Thank you!