Skip Navigation

[Resolved] Query Repeating Field Group values in order

This thread is resolved. Here is a description of the problem and solution.

Problem:
Query Repeating Field Group values in order

Solution:
For RFG Fields, the sort order is stored in database table postmeta with meta key = "toolset-post-sortorder".

You can use Types post relationship API function 'toolset_get_related_posts' in order to get repeating field group items.

For example:

  $child_columns = toolset_get_related_posts(
  $post->ID,     // the parent post
  'columns-layout',   // the RFG slug
  'parent',     // the RFG role in this relationship is 'child'
  100,     // the maximum number of results
  0,           // the offset
  array('meta_key'=>'toolset-post-sortorder'),     // additional query arguments
  'post_object',   // return format
  'child',    // role to return
  'meta_value_num',
  'ASC'
  );

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/query-repeating-field-group-values-in-order/#post-923583

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

100% of people find this useful.

This support ticket is created 6 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 3 voices.

Last updated by benE-2 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#921769

I have a repeating field group on a custom post type.

I'm querying the values using the toolset_get_related_posts() function with "post_object" so that it returns the whole post objects.

The values it returns are unordered/do not match the order of the entries on the custom post type.

E.g. If I add 2 instances of the repeating field group on my post, "values A" and "values B", and then I switch the order in the wordpress backend so that "Values B" appears before "Values A", then when I query with "toolset_get_related_posts" the array of posts is not in the order you see in the wordpress backend, and there appears to be no way of sorting it.

I want the array of posts I get to be in the same order that is specified in the parent post.

#922025

toolset_get_related_posts has it's own mandatory order and sort by parameters, where defaults are:
$orderby = null
$order = 'ASC'

This follows the WordPress Query and acts on the Post, not it's position in the Group.
Hence, it should return you a commonly ordered list (as if you would receive native posts by those parameters unless modified by WP Query rules).

This means you need to:
- know how Toolset stores the position of the RFG field
- then apply that to the query.

In old Repeating fields that order was simply added by a hidden custom field, and used to be a simple numeric value.

In new RFG Fields, this should not be different and is the Custom Field "toolset-post-sortorder"
You can doublecheck that in any View that queries the Repeating Field Groups.
When you order those by that field, and alter the backend order of such field in a post, then this reflects in the ordering of the View output.

In code, you may want to address this like a native WordPress Custom Meta Field, with key "toolset-post-sortorder", value (numeric) and the usual post and meta ID.

toolset_get_related_posts() is not really made to query RFG, it's made to get related posts as in a Post Relationship.
I see it supported only in toolset_get_relationships() and toolset_get_relationship().

Could you let us know what exactly you need to achieve, that cannot be done in the related RFG Views?

Thank you!

#922638

The site in question isn't using views, only the types plugin, so we need a way of querying a RFG for a post from within our custom templates.

What is the preferred way for doing this if it's not using toolset_get_related_posts() ?

For example, we currently have in our templates a :

$child_columns = toolset_get_related_posts(
                $post->ID,
                'columns-layout',
                'parent',
                                100,
                                0,
                                array(),
                                'post_object'

        );

foreach($child_columns as $column)
	{
         ...do stuff related to rendering the information - order matters, $child_columns should be in the order of the RFG in the wordpress backend.

        }

where columns-layout is an RFG.

#922938

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the proffered way is to use views as you can see with our Docs:
=> https://toolset.com/documentation/getting-started-with-toolset/creating-and-displaying-repeatable-field-groups/#displaying-repeatable-field-groups

What if you create a view that displays the repeatable field group and use the do_shortcode() function to display the view?

For example:

echo do_shortcode('[wpv-view name="your-view"]');
#923106

I'd rather not start integrating views to get this working. Is there no other way? How does views do it?

I'm guessing we could manually start unpicking/querying the underlying posts ourselves to get them in order, but I'd rather not do that if there's a better way.

#923583

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - For RFG Fields, the sort order is stored in database table postmeta with meta key = "toolset-post-sortorder".

what if you try to use following code:

  $child_columns = toolset_get_related_posts(
  $post->ID,     // the parent post
  'columns-layout',   // the RFG slug
  'parent',     // the RFG role in this relationship is 'child'
  100,     // the maximum number of results
  0,           // the offset
  array('meta_key'=>'toolset-post-sortorder'),     // additional query arguments
  'post_object',   // return format
  'child',    // role to return
  'meta_value_num',
  'ASC'
  );

foreach($child_columns as $column){
       ///    ...do stuff related to rendering the information - order matters, $child_columns should be in the order of the RFG in the wordpress backend.
 }

It should work.

#923611

Yes, that has worked! Thank you!