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