Skip Navigation

[Resolved] Displaying the unique value of a custom field in a view

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 3 replies, has 1 voice.

Last updated by Saul Baizman 6 days, 14 hours ago.

Assisted by: Minesh.

Author
Posts
#2792411

Hi there,

I have a custom post type with two custom fields, "year" and "month". There are about 118 posts in total, approximately one per month since 2012.

I would like to display a list of the unique years (2012, 2013, 2014, etc.). Typically, views are used to display posts, but I want to display the unique year values from among all 118 posts. Is that possible? If so, how?

From an architectural perspective, is it *advisable* to use a custom field for this purpose, or would it be wiser to consider a custom taxonomy instead?

Thanks.

Saul

#2792413

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

For ease of use, yes, I would say taxonomy is better so you can easily identify to what year the post belongs to.

With custom field we can do that that that will require a bit of custom code to group the posts by year.

Please let me know how you wouild like to proceed further.

#2792525

Minesh,

Thanks for your response. I haven't decided whether to use the taxonomy or custom field. The interface for the taxonomy—choosing multiple items—is undesirable when only one item should be selected, as in a pull-down menu.

Could you provide the custom code to group the posts by a custom field (year)?

Thanks.

Saul

#2792592

Minesh
Supporter

Languages: English (English )

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

First thing you edit your view and set your view to order by your year custom field (ASC/DESC) as per your requirement.

Then, you should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-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 '';
}

Within your view's loop you should try to add the following:

<wpv-loop>
          [heading condition="year" value="[types field='year-field-slug' output='raw'][/types]"]
            <h4>[types field='year-field-slug' output='raw'][/types]</h4>
        [/heading]
                  /// add here any other fields you want to display
                 <li>    <h4>[wpv-post-title]</h4> </li>
      
        </wpv-loop>

Where:
- Replace "year-field-slug" with your original year custom field slug.

More info:
- https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

#2793169

This is so helpful, Minesh! Thank you.

I ultimately decided to just use the native post date field. This required creating a custom shortcode. The shortcode runs a SQL query to select the unique years and passes each year to a view as an attribute via the render_view() Toolset API function. Here's the SQL query in case it's of interest to others:

$query = "SELECT distinct(SUBSTRING(post_date,1,4)) AS aotm_year FROM {$wpdb->prefix}posts WHERE post_status='publish' AND post_type='aotm' ORDER BY aotm_year DESC";

It works great!

Thanks again.