Skip Navigation

[Resolved] How to get ordered fields from RFG?

This support ticket is created 5 years, 6 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 10 replies, has 2 voices.

Last updated by Christian Cox 5 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1385189

I got 2 fields in group:
youtube_id 1: TeBW-WByzlw 144 163
youtube_id 2: CEdYPEKDD2E 255 270

This is how I try to get fields:

$videoId_arr = array();
$child_posts = toolset_get_related_posts( get_the_ID(), 'playlist_item', array( 'query_by_role' => 'parent', 'return' => 'post_object' ));
foreach ($child_posts as $child_post) {
$videos_ids = types_render_field( "youtube_id", array( "id"=> "$child_post->ID") );
$videos = explode(" ", $videos_ids);
array_push($videoId_arr, $videos[0]);
}

This is result with wrong order:
Array [ "CEdYPEKDD2E", "TeBW-WByzlw" ]

Please help me fix it.

#1385269

Hello, it sounds like you want to sort the results based on the order of the RFG in the parent post. Is that correct? If so, then you can use the toolset-post-sortorder meta_key and a meta_value_num orderby argument in your toolset_get_related_posts API call. Here is an example:

$child_posts = toolset_get_related_posts( 
  get_the_ID(), 
  'playlist_item', 
  array( 
    'query_by_role' => 'parent',
    'return' => 'post_object',
    'meta_key' => 'toolset-post-sortorder',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  )
);

Please let me know if I have misunderstood, or if the code is not working as expected.

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#1385285
Screenshot_32.png

The screenshot shows the order of the fields in the group when editing, but when I try to get the fields in the frontend, I can not get the same order of the fields. I assume the problem is in this line:

$videos_ids = types_render_field( "youtube_id", array( "id"=> "$child_post->ID") );

Sorry for my English...

#1385295

I assume the problem is in this line:
$videos_ids = types_render_field( "youtube_id", array( "id"=> "$child_post->ID") );

The $videos_ids has nothing to do with the order:

Array [ "TeBW-WByzlw", "CEdYPEKDD2E" ]

The order of the results is determined by the orderby and order arguments in the toolset_get_related_posts API call. Please copy + paste the complete code so I can review it.

#1385299

<?php
$videoId_arr = array();
$child_posts = toolset_get_related_posts( get_the_ID(), 'playlist_item', array( 'query_by_role' => 'parent', 'return' => 'post_object' ));
foreach ($child_posts as $child_post) {
$videos_ids = types_render_field( "youtube_id", array( "id"=> "$child_post->ID"));
$videos = explode(" ",$videos_ids);
array_push($videoId_arr, $videos[0]);
} ?>

<script>
var videoId_arr = [
<?php foreach ($videoId_arr as $value) {
echo "'".$value."',";
}?>];

console.log(videoId_arr);
</script>

In console:
Array [ "CEdYPEKDD2E", "TeBW-WByzlw" ]
the order is different

#1385301

As I said, you must modify the toolset_get_related_posts function to include the order, orderby, and meta_key arguments:

$child_posts = toolset_get_related_posts( 
  get_the_ID(), 
  'playlist_item', 
  array( 
    'query_by_role' => 'parent',
    'return' => 'post_object',
    'meta_key' => 'toolset-post-sortorder',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  )
);

Please change the code as suggested.

#1385303
Screenshot_33.png

Sorry but when i did it my console began to look like in the screenshot and finally here is all my code))
<?php
$video_titles = array();
$videoId_arr = array();
$startSeconds_arr = array();
$endSeconds_arr = array();
$child_posts = toolset_get_related_posts(
get_the_ID(),
'playlist_item',
array(
'query_by_role' => 'parent',
'return' => 'post_object',
'meta_key' => 'toolset-post-sortorder',
'orderby' => 'meta_value_num',
'order' => 'ASC'
)
);
foreach ($child_posts as $child_post) {
$video_title = types_render_field( "new_event", array( "id"=> "$child_post->ID"));
array_push($video_titles, $video_title);
$videos_ids = types_render_field( "youtube_id", array( "id"=> "$child_post->ID"));
$videos = explode(" ",$videos_ids);
array_push($videoId_arr, $videos[0]);
array_push($startSeconds_arr, $videos[1]);
array_push($endSeconds_arr, $videos[2]);
} ?>
<script>
var videoTitle_arr = [
<?php foreach ($video_titles as $value) {
echo "'".$value."',";
}?>];
var videoId_arr = [
<?php foreach ($videoId_arr as $value) {
echo "'".$value."',";
}?>];
var startSeconds_arr = [
<?php foreach ($startSeconds_arr as $value) {
echo "'".$value."',";
}?>];
var endSeconds_arr = [
<?php foreach ($endSeconds_arr as $value) {
echo "'".$value."',";
}?>];
console.log(videoTitle_arr);
console.log(videoId_arr);
console.log(startSeconds_arr);
console.log(endSeconds_arr);
</script>

#1385313

Okay I see I had an error in my code example. Please try this update instead:

$video_titles = array();
$videoId_arr = array();
$startSeconds_arr = array();
$endSeconds_arr = array();
$child_posts = toolset_get_related_posts(
get_the_ID(),
'playlist_item',
array(
'query_by_role' => 'parent',
'return' => 'post_object',
'args' => array(
        'meta_key' => 'toolset-post-sortorder',
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
      )
)
);
#1385315
Screenshot_35.png
Screenshot_34.png

Ok now i can get an array, but the order is still not correct. Could this be due to an outdated version of the plugin?

#1385339

I don't think so, but you should always use the latest versions of the plugins. May I log in and see how this is set up? Please let me know where I can see the results on the front-end of the site.

#1385435

1. I was able to log in once, but this User is not an admin so I cannot see the Toolset information.
2. After logging out, I can not log in again.
3. I do not have full access in FTP and I cannot find the code in the plugin files. Can you help?