Skip Navigation

[Resolved] How to output custom fields in an array?

This support ticket is created 4 years, 2 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)

This topic contains 2 replies, has 2 voices.

Last updated by StanleyT8485 4 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1815249

Tell us what you are trying to do?
I want to output the custom fields of child posts in an array.

	$postID = get_the_ID();
	$query = new WP_Query( array(
		'post_type' => 'financial', // This is the child post
		'toolset_relationships' => array(
			'role' => 'child',
			'related_to' => $postID,
			'relationship' => 'company-financial', // This is the Toolset slug relationship
		),
	));
	while( $query -> have_posts() ) {
		$query -> the_post();
		$postID = get_the_ID();
		$pageCF = get_post_custom( $postID );
		$distributions = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
		print_r( $distributions );  // This prints out Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )
	}

I want it to output in this format 11.7, 23.8, 26.7, 36.8, 28.6, 29, 30.3.

However, the result is structured in this format
Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )

#1815987

Hello,

For example, you can relace these lines from:

while( $query -> have_posts() ) {
    $query -> the_post();
    $postID = get_the_ID();
    $pageCF = get_post_custom( $postID );
    $distributions = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
    print_r( $distributions );  // This prints out Array ( [0] => 11.7 ) Array ( [0] => 23.8 ) Array ( [0] => 26.7 ) Array ( [0] => 46.9 ) Array ( [0] => 28.6 ) Array ( [0] => 29 ) Array ( [0] => 30.3 )
}

To:

$distributions = array();
while( $query -> have_posts() ) {
    $query -> the_post();
    $postID = get_the_ID();
    $pageCF = get_post_custom( $postID );
    $distributions[] = $pageCF[ 'wpcf-distributions' ]; // The custom field I want to output is wpcf-distributions
}
print_r( $distributions );

More help:
hidden link

#1817557

My issue is resolved now. Thank you!