Skip Navigation

[Resolved] set variable with custom fields from child posts – comma separated and quotes

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

This topic contains 8 replies, has 2 voices.

Last updated by Luit 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1942063

I created the shortcode below. I want to create a list with the values of custom fields from child posts.

The output should be for example:
'145', '170', '188'

The shortcode below only returns the first value, so 145. What's wrong with the code below?

add_shortcode('display_fields', 'display_fields_func');
function display_fields_func ($atts){

$origin_id = get_the_ID(); // Post id of the parent post of type 'persoon'
$relationship_slug = 'relatie_personen_phvs'; // relationship slug between persoon and phv

$child_posts = toolset_get_related_posts(
$origin_id, // get childs 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
'orderby' => 'title',
'order' => 'ASC',
'need_found_rows' => false, // also return count of results
'args' => null // for adding meta queries etc.
)
);

foreach($child_posts as $id){
$array = get_post_meta( $id, 'wpcf-phv_lengte');
$list = implode(', ', $array);
}
return $list;
}

#1942513

Hello, there could be one or more problems here. First, I would try to determine if the toolset_get_related_posts query is returning the correct values. To test that, I would try this code modification:

add_shortcode('display_fields', 'display_fields_func');
function display_fields_func ($atts){

  $origin_id = get_the_ID(); // Post id of the parent post of type 'persoon'
  $relationship_slug = 'relatie_personen_phvs'; // relationship slug between persoon and phv

  $child_posts = toolset_get_related_posts(
    $origin_id, // get childs 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
      'orderby' => 'title',
      'order' => 'ASC',
      'need_found_rows' => false, // also return count of results
      'args' => null // for adding meta queries etc.
    )
  );

  // test child_posts array
  $child_ids = implode(', ', $child_posts);
  return $child_ids;

  foreach($child_posts as $id){
    $array = get_post_meta( $id, 'wpcf-phv_lengte');
    $list = implode(', ', $array);
  }
  return $list;
}

The shortcode should now return a comma-separated list of child post IDs, like this:

123, 456, 789

Each number should represent one child post. If your parent post has three child posts, you should see three child IDs output by the shortcode. If you do not see the three child post IDs, you know there is a problem with the post relationships API query. If you see the correct child post IDs, you know the problem is in the foreach loop after the query.

Once the results of the post relationships query are confirmed, you can delete the code I added:

  // test child_posts array
  $child_ids = implode(', ', $child_posts);
  return $child_ids;

In the foreach loop, it looks like you are resetting the values of $array and $list each time the loop iterates. It seems like you should be appending a value to $array in each loop iteration, then you should implode the list AFTER the loop is complete. Something like this:

  // initialize an empty holder array
  $array = array();
  foreach($child_posts as $id){
    $array[]= get_post_meta( $id, 'wpcf-phv_lengte');
  }
  $list = implode(', ', $array);
  return $list

But this depends on whether or not the phv_lengte field allows multiple values. I cannot tell exactly what you're trying to achieve here based on the information I have.

#1942533

Ths comma separated list of post id's was visible. After deleting this code and changing the last part where the array is filled I get
the following output: Array, Array, Array etc.

In the debug .log:
Array to string conversion in /var/www/vhosts/groeiberekenen.nl/httpdocs/wp-content/toolset-customizations/test_calculate_fields.php on line 42

My snippet is now:
add_shortcode('display_fields', 'display_fields_func');
function display_fields_func ($atts){

$origin_id = get_the_ID(); // Post id of the parent post of type 'persoon'
$relationship_slug = 'relatie_personen_phvs'; // relationship slug between persoon and phv

$child_posts = toolset_get_related_posts(
$origin_id, // get childs 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
'orderby' => 'title',
'order' => 'ASC',
'need_found_rows' => false, // also return count of results
'args' => null // for adding meta queries etc.
)
);

// test child_posts array
// $child_ids = implode(', ', $child_posts);
// return $child_ids;

// initialize an empty holder array
// initialize an empty holder array
$array = array();
foreach($child_posts as $id){
$array[]= get_post_meta( $id, 'wpcf-phv_lengte');
}
$list = implode(', ', $array);
return $list;
}

#1942857
Screen Shot 2021-02-11 at 3.27.23 PM.png

Does the phv_lengte field allow only one value, or multiple values?
You would be able to tell whether it's a single field or a field that allows multiple values in Toolset > Custom Fields > Post Fields when you edit this custom field. See the screenshot here.

#1942899

Only 1 value

#1942977

I managed to solve the issue by putting true at the end of the get_post_meta.
foreach($child_posts as $id){
$array[]= get_post_meta( $id, 'wpcf-phv_lengte', true);
}
$prelist = implode("', '", $array);
$list = "'".$prelist."'";
return $list;

#1942981

One other question, phv_lengte is a numeric field. I also need a date field but am unable to get the right output as the format is in datetime so a number. I want to get output like: 31-01-2021, 04-02-2021, etc

I tried to use some date commands with formats, but I am not able to get the right output.

foreach($child_posts as $id){
$array[]= get_post_meta( $id, 'wpcf-phv_testdatum', true);
}
$prelist = implode("', '", $array);
$list = "'".$prelist."'";
return $list;

#1942999

Solved this one also:

$array_testdatum[]= date ("d-m-Y", (get_post_meta( $id, 'wpcf-phv_testdatum', true)));

Thanks for all your help!

#1943001

My issue is resolved now. Thank you!