Skip Navigation

[Résolu] How to access single fields from a CPT with relationship in php

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

The issue here is that the use wanted to display custom fields values from their Parent/Child Posts on the frontend

Solution:
If it is a custom field value on a related item then you can use our [types field='my-field'][/types] shortcode with the item attribute.

See our documentation below on the item attribute.
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

This support ticket is created Il y a 2 années et 10 mois. 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 4 réponses, has 2 voix.

Last updated by fred-r.M Il y a 2 années et 10 mois.

Assisted by: Shane.

Auteur
Publications
#2095447

Tell us what you are trying to do? We are having tours (products) and a CPT reviews. I like to get single fields using a short code to get different information - based on the field attribute.

Is there any documentation that you are following? https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Is there a similar example that we can see? Not yet...

But what I tried to do is the following code, but it not working:

function get_my_review($atts) {
  
  global $post;
  $str = '';
  $origin_id = $post->ID;
  $relationship_slug = 'my-tour-id';
  $field_value = get_post_meta($post->ID,'wpcf-'.$atts['field'],true);
  
  $results = toolset_get_related_posts ( 
        // get posts related to this one
        $origin_id, // get posts connected to this one
        $relationship_slug, // in this relationship
        array(
        	'query_by_role' => 'parent', // origin post role
        	'role_to_return' => 'child', // role of posts to return
        	'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
        	'limit' => 999, // max number of results
        	'offset' => 0, // starting from
        	'args' => array(
                'meta_key'  => $field_value,
            ),
    	    )
        );
  
        if($atts['field']=='author-first-name' and !empty($field_value)) {
			$str .= $field_value;
		}
		return $str;
}
/* use on template [show_my_review field="author-first-name"] */
add_shortcode( 'show_my_review', 'get_my_review' );

What is the link to your site?

#2096069

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

Thank you for getting in touch. You may not need to write a custom shortcode for this.

If it is a custom field value on a related item then you can use our [types field='my-field'][/types] shortcode with the item attribute.

See our documentation below on the item attribute.
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

Please let me know if this helps.
Thanks,
Shane

#2096213

Dear Shane

I would love to have only [types field='my-field'][/types] - how do I add the attribute? like this:

[types field='my-field' attribute='@product-review.child'][/types]
#2096997

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

Assuming the relationship slug information is correct then you should use "item" as the attribute like below.

[types field='my-field' item='@product-review.child'][/types]

Please let me know if this helps.
Thanks,
Shane

#2097759

My issue is resolved now. Thank you!

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