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.
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
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...
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.
<?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
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.
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>
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'
)
)
);
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?
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.
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?