Skip Navigation

[Resolved] Grouping by post reference field does not work

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

Problem: I would like to group posts in a View by post reference field's post title.

Solution: Update the custom code to group by the post reference field raw value, which is a post ID. Then display the title of the post reference field.

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

Last updated by johnnyH-3 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1128267

I am trying to group a list by a post reference field, similar to what we achieved here: https://toolset.com/forums/topic/view-of-upcoming-events-but-w-dates-distance-from-2-related-child-post-types/#post-1126170

It works fine if I group by a parent field in a post relationship. But it does not work if I'm trying to do the same but using a post reference field instead.

Here's the code I have in the view template loop for now:

[group-title condition="title" value="[wpv-post-title item='@race_event.parent']"]
	<li>[wpv-post-title item="@race_event.parent"]</li>
[/group-title]

The function for the [group-title] shortcode looks like this:

add_shortcode('group-title', 'func_group_by_display_title');
function func_group_by_display_title($atts, $content = '') {
  static $title = null;
   
  $condition = $atts['condition'];
  $value = $atts['value'];
  switch ($condition) {
    case 'title':
      if ($$condition != $value) {
        $$condition = $value;
        return $content;
      }
      break;
  }
  return '';
}

From counting the number of list items it does seems that it groups by the same Post Reference fields. But the actual values of the Post Reference field are not output... If I remove the shortcode it outputs a list of all the fields with the post title from the Post Reference field. But when I add the shortcode, nothing.

Can I not use a post reference field in such a way or do I have to do it differently?

I'm working with the view called "Test", with output on this page: hidden link

#1128377

Hi, I updated the shortcode a bit for you to compare based on the post reference field value, which is a numeric ID. Here's the updated code:

[group-title condition="title" value="[wpv-post-field name='wpcf-race_event']"]
	<li>[wpv-post-title id="[wpv-post-field name='wpcf-race_event']"]</li>
[/group-title]

Can you check to confirm it's grouping as expected?

#1128943

Perfect! That works great.