[Resolved] How can I group views by custom field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by Adriano 5 years, 9 months ago.

Assigned support staff: Adriano.

Author
Posts
#225429

Hi,
I'm wondering, how can I group views by custom field?
Regards,
Michael

#225486

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Michael,

There is a section called "Ordering" in the Views edit page where you can choose a custom field to order by. If this custom field is not there, it means it is hidden. So, you must go to Views > Settings > Hidden custom fields, there you can make them visible.

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#225566

Hi Adriano,
thanks for your answer. I could set the order by my custom field, but how I print this field. I will explain my case:

I have a custom post type "seminar" and created a custom field 'time' for them with select options(Fr 10-11am, Fr 11-12am, Sa 10-11 am....), so when I display the list of seminars I'm getting following result:

Friday 10-11am - Seminar 1
Friday 10-11am - Seminar 2
Friday 10-11am - Seminar 3
Sunday 10-11am - Seminar 4
Sunday 10-11am - Seminar 5

and I would like to get following result:

Friday 10-11am
Seminar 1
Seminar 2
Seminar 3

Sunday 10-11am
Seminar 4
Seminar 5

How can I achieve this with types and views?

Regards,
Michael

#225656

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Michael,

Ok, I see what you mean. Unfortunately it's not possible to group by a custom field for now. But I have a workaround for you you may like.

Using two views you can build a loop like a "group by". You will need a view to list all taxonomies and another one to list all posts inside of a taxonomy. So you will need to convert "time" custom field to a taxonomy. Please follow steps below and let me know if you have any doubt:

1) Create a View and call it "Times"
2) In "Content selection" choose the created taxonomy "Time"
3) In "Layout HTML/CSS/JS" insert the code below:

<wpv-loop>
      [wpv-taxonomy-link]
</wpv-loop>

4) Create a new View and call it "Seminars"
5) In "Content selection" choose "Seminar" post type
6) In "Query filter" add a filter by taxonomy "Time", choose the option "Value set by View shortcode attribute" and let value "Taxonomy slug" selected. Give a name "time" in "Shortcode attribute"
7) In "Layout HTML/CSS/JS" add the following code:

<ul>
      <wpv-loop>
         <li>[wpv-post-title]</li>
      </wpv-loop>
</ul>

8) We are almost there, you have both views done. Now you need to edit the first view "Times" again to edit its "Layout HTML/CSS/JS" section. We will add a call to the second view "Serminars". Replace its code with:

<wpv-loop>
      [wpv-taxonomy-link]
      [wpv-view name="Seminars" time="[wpv-taxonomy-slug]"]
</wpv-loop>

9) Create any page and add a call to the Times view and you will see it working:

      [wpv-view name="Times"]

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#225687

Wow, nice workaround 🙂 Many thanks, it works and would be a great solution if it would end by this, but I still need to add parametric search, to filter the seminars by category(custom select field).

Unfortunately I can't add parametric search to taxonomy view and when I add it to 'Seminars' I'm getting for every group one parametric search, so for 8 groups I will have 8 parametric searches, what will not look nice.

Do you know the solution with grouping the post by date? Here the link and below the steps and code: https://toolset.com/forums/topic/grouping-posts-by-yearmonth-of-publish-date/

1. create a view that will list all your News custom post type and order by post date descending.
2. insert in the Layout HTML in the loop the following custom short codes:

[heading condition="year" value="[wpv-post-date format="Y"]"]
<h4>[wpv-post-date format="Y"]</h4>
[/heading]
[heading condition="month" value="[wpv-post-date format="F"]"]
<h5>[wpv-post-date format="F"]</h5>
[/heading]

3. insert in your theme functions.php file the following code:

add_shortcode('heading', 'my_heading');
function my_heading($atts, $content = '') {
  static $year = null;
  static $month = null;
  $condition = $atts['condition'];
  $value = $atts['value'];
  switch ($condition) {
      case 'year':
      case 'month':
        if ($$condition != $value) {
            $$condition = $value;
            return $content;
        }
        break;
  }
  return '';
}

Is there a way to make it works for custom field?

I think I could use it for my seminars times and replace the year with the day and month with the hours, but I still need 'from... to' timing.

What do you think?

Regards,
Michael

#226207

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Michael,

1) Unfortunately you can not make a parametric search works with all groups at once. Each group is a different query, parametric search is built to works with only one main query. So, as you said above, you will need one form for each group, which is not so good indeed.

2) Ana's solution is nice, but as the previous question, it will only works fine in a query. This one you are creating has many queries, one for each group.

Sorry, but as you can see, we don't have a native feature to group posts by something. That's just a workaround, you can not integrate so much things to it. We should improve that in the future.

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#227230

Hi Adrian,
thank you for your support. I was able to make it works with Ana's work-around and one query with date fields. Here the result:

hidden link

Regards,
Michael

#227259

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Nice, you are welcome.

The forum ‘Types Community Support’ is closed to new topics and replies.