Skip Navigation

[Resolved] View Block on Content Template – achieve (pseudo) Group By functionality

This support ticket is created 3 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 3 replies, has 2 voices.

Last updated by Christian Cox 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1987895

Tell us what you are trying to do?
I have a Custom Post Type of Training Courses, and a Custom Post Type of Testimonials, linked by a One-to-Many relationship. On any training course page, at the bottom, I want to show testimonials that have been given for the course. I can make this work by creating a View Block on the Training Course Content Template, no problem.

However, if I explain the fields in CPT Testimonials, they are basically a quote (the testimonial), and who sent it in (name, company name). As there can be many testimonials from a single training class that has been run, it's repetitive and doesn't look very good for the view to list all quotes, followed by the same company name. What I would like to do is be able to loop through all quotes for the same company, and then, after the last one in the group, display the company name.

So NOT THIS:
Great Course!
ABC Inc

Wish I had done this sooner.
ABC Inc

I struggled a little but got there in the end thanks to the great tutor.
ABC Inc

BUT THIS:
Great Course!
Wish I had done this sooner.
I struggled a little but got there in the end thanks to the great tutor.
ABC Inc

In other words, I only want to mention the Company name after the last one in the group. Other groups may follow, as the training course is run repeatedly, and therefore there will be groups of testimonials from more companies.

Is there any documentation that you are following?
Read various threads and articles, but can't relate what I've read with working in the block editor

Is there a similar example that we can see?
No

What is the link to your site?
Here is a page where you can see how it is coming out - scroll to the bottom of the page, under the 3 coloured calls to action.
hidden link
This is a password-protected test site. Let me know how to pass you the credentials.

Thanks for your help.

Steve

#1988279

Hello, I understand what you're asking for but unfortunately there is nothing exactly like this built-in to the Block Editor (or the legacy Views editor either, for that matter). There is no concept of "grouping" the results of a View unless you're using some kind of nested View structure. Unfortunately I don't think a nested View structure is appropriate in this case. Any solution here is going to require some custom code. If you are comfortable writing JavaScript / jQuery, you could write a custom function that loops over all the results backwards and compares the current company name to the previous company name. Based on that comparison, show or hide the current company name. I don't have such a snippet available, unfortunately.

On the other hand, I have a custom code snippet available now that would show the company name before the grouped quotes, as opposed to showing the company name after grouped quotes:

ABC Inc
Great Course!
Wish I had done this sooner.
I struggled a little but got there in the end thanks to the great tutor.

DEF Inc
Awesome educational materials!
This really helped.
The tutors are exceptional.

...etc...

I can help you customize the snippets from another ticket to group the results of your View of testimonials: https://toolset.com/forums/topic/grouping-posts-by-yearmonth-of-publish-date/

Let me know how you would like to proceed.

#1988969

Hi Christian,

Thank you for your thoughts and for pointing me at the example. I can probably follow that, and I think I had read that one before. But where does the wpv-layout-start/end block get defined exactly? Remember, I am working with a content template with an embedded View block. Do I discard the View block and somehow add my variation of this example? Maybe if you could show me the functions.php snippet for my shortcode (which would be CompanyName, I guess), that would be helpful.

Thanks.

Steve

#1989195

But where does the wpv-layout-start/end block get defined exactly?
Basically it corresponds to the View's loop block in the block editor, where you define the design for a single loop item. You would have to implement the company name with shortcode(s) instead of blocks. The quote can remain in the design as block(s), but the company name should be rendered by shortcode. You would delete whatever blocks are currently used to display the company name in the loop, and replace those blocks with something like a custom HTML block. Add the custom shortcode and include the company name inside the custom shortcode contents. See below.

Maybe if you could show me the functions.php snippet for my shortcode (which would be CompanyName, I guess), that would be helpful.
I suggest changing the shortcode name to something less generic, like company-heading. Assuming the company name is a custom field with the slug "company", the custom HTML block contents would look something like this:

[company-heading value="[types field='company' output='raw'][/types]"]
    <h4>[types field='company'][/types]</h4>
[/company-heading]

You can customize the contents inside the company-heading shortcode as needed to produce whatever markup you want to display the company name.

The PHP code could be modified as follows to produce the custom shortcode and "grouped" results:

global $company;
$company = '';
add_shortcode('company-heading', 'company_heading');
function company_heading($atts, $content = '') {
  global $company;
  $a = shortcode_atts( array(
      'value' => ''
  ), $atts );
  $value = $a['value'];
  if ($company != $value) {
    $company = $value;
    return $content;
  }
  return '';
}