Sauter la navigation

[Résolu] How to get value of a field from a custom post with parent post reference field

This support ticket is created Il y a 3 années et 6 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.

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/Karachi (GMT+05:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par fred-r.M Il y a 3 années et 6 mois.

Assisté par: Waqar.

Auteur
Publications
#2079015

Tell us what you are trying to do? We are having products (WooCommerce) and custom posts Tour Dates. In the tour dates we are having fields I need to get the values.

Both post types are "linked" with a post reference field (where the parent post is referenced).

How do I get values (as php code) from the tour dates custom post type?

Is there any documentation that you are following? I found this, https://toolset.com/forums/topic/get-post-parent-witn-new-relationship-model-php/, but not sure if this is the correct "document".

Is there a similar example that we can see?

What is the link to your site?

#2079347

Dear Waqar

Okay. Do You think this will work?

funtion get_my_tour_date_id () {

	global $post;
    $parent_post = $post->ID;
    $field_slug = 'tour-date-field';
 
	$results = toolset_get_related_posts( 
		// get posts related to this one
		$parent_post,
 
		// Relationship between the posts
		$field_slug,
 
		// Additional arguments
		[
			// This is mandatory because we're passing just a single post as the first parameter.
			'query_by_role' => 'child', 
			// pagination
			'limit' => 9999, 
			'offset' => 0,
			'args' => '',
			'role_to_return' => 'other',
			'return' => 'post_id'
		]
	);
 
	return $results;
}

If I like to get all date values out - are I am able to use this function, where I used to get the id from the tour_dates?

#2080371

Thanks for writing back.

To confirm whether this code will work or not, I'll need to see how these post types and fields are set up on your website.

Can you please share temporary admin login details? This will allow me to test the code with a similar setup on my test website.

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

#2081537

Thank you for sharing these details.

Here is a quick example of a shortcode that gets all the related (through post reference field) "Tour Dates" posts of the current "Product" post and then prints their 3 custom field values:


add_shortcode('get_related_TD_fields', 'get_related_TD_fields_func');
function get_related_TD_fields_func() {

	global $post;
	$parent_post = $post->ID;
	$field_slug = 'tourid';
	
	$get_results = toolset_get_related_posts( $parent_post, $field_slug, 'parent', 1000, 0, array(), 'post_id','child');

	ob_start();
	foreach ( $get_results as $result ) {
		$tour_date_field = types_render_field( 'tour-date-field', array( 'item' => $result) );
		echo "tour_date_field: ".$tour_date_field."<br>";
		$tour_price = types_render_field( 'tour-price', array( 'item' => $result) );
		echo "tour_price: ".$tour_price."<br>";
		$supplement = types_render_field( 'supplement', array( 'item' => $result) );
		echo "supplement: ".$supplement."<hr>";
	}
	return ob_get_clean();

}

You can adjust the output of this shortcode as needed and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2082167

My issue is resolved now. Thank you!