Advanced Views Docs
Live PlaygroundDownload PluginGet PRO
  • Help Centre
  • Getting Started
    • Introduction
      • Key Aspects
      • Creating Your First View
      • Creating Your First Card
      • Plugin Terms
      • Plugin Interface
        • Field Options
    • Installation
      • Advanced Views Lite
      • Advanced Views Pro
      • Comparison Table
      • Upgrading from Lite to Pro
    • Supported data vendors
    • Starter theme
  • Display Content
    • WordPress
      • Post
      • Taxonomy terms
      • User
      • Comments
      • Menus
    • WooCommerce Product
    • Meta fields
      • Basic fields
        • Number
      • Content fields
        • Image
        • File
        • WYSIWYG
        • oEmbed
        • Gallery
      • Choice fields
        • Select/checkbox/radio
        • True / False
      • Relationship fields
        • Link
        • Post_object
        • Page Link
        • Relationship
        • Taxonomy
        • User
      • Advanced fields
        • Google Map
        • Google Map Multiple Markers
        • ACF OpenStreetMap Field
        • Date/time picker
      • Layout fields
        • Group (Pro)
        • Repeater (Pro)
        • Flexible (Pro)
    • Object fields
    • Custom Data (Pro)
    • Custom Gutenberg Blocks (Pro)
    • Mount Points (Pro)
    • Front-end assets management (Pro)
  • Query Content
    • Basic Filters
    • Meta Filters (Pro)
      • Current Post
      • Comparison
      • Dynamic Injection
    • Taxonomy Filters (Pro)
    • Pagination (Pro)
    • Custom Data (Pro)
  • Shortcode Attributes
    • Common Arguments
    • View Shortcode
    • Card Shortcode
  • Templates
    • Customizing the Template
    • Template engines
      • Twig
      • Blade
    • CSS & JS
      • BEM Methodology
      • WordPress Interactivity API
    • File system storage
    • Pre-built components
    • Reusable components library (Pro)
    • Live reload
    • Multilingual
    • Custom Ajax & Rest API (Pro)
  • Guides
    • Display Grouped Posts
    • Display Custom Post Type (CPT) on another post
    • Display Employees (CPT) of a Company (CPT)
    • Display Nested Repeater Fields
    • Map Marker from ACF Image
    • Display all CPT items on a single map
  • Tools
    • Export/Import
    • Demo Import
    • Settings
    • Preview
  • Troubleshooting
    • Compatibility
    • Report a bug
    • Payment Issues
    • Lite Support (Forum)
    • Pro Support
  • Customization
    • Filters and Hooks
      • View
      • Card
    • Suggest a feature
    • Performance
Powered by GitBook
On this page
  • In this example
  • Step by step guide

Was this helpful?

  1. Guides

Display Grouped Posts

An Advanced Guide for Pro users to help them through step-by-step on how to display lists of information grouped by a field.

In this example

We'll take a list of company employees, and display them in an alphabetical ordered list with the Last name in Ascending order, and include a line with first letter of the last name.

Step by step guide

  1. First create your Field Group with the fields, in our case it's text fields for Last Name and First Name assigned to a Custom Post Type (CPT) called Employees.

  2. Visit the Advanced Views tab in WordPress backend.

Create your View

  1. Click the Add New button to add a new View and Enter a Name for your View.

  2. Click the Add Field button in the Fields section. Then select the target Group from the list and the Field from the dropdown. Continue to Add Fields until they've all been assigned. in our case we're only adding two fields, Last Name and First Name.

  3. Click on the Publish button to save and publish your View.

  4. Optional — Continue editing your View, switch to the Template tab and copy the Default Template into the Custom Template field, add in a comma, so names are displayed nicely. e.g. Doe, John

Create your Card

  1. Use the "Create card" link in the right sidebar of your View, or Simply go to Card top tab.

  2. Click the Add New button to add a new Card, enter a Name for your Card.

  3. On the General tab you should choose Sort by "Meta value", with the Sort by Meta Field Group to the one containing the name fields, in our case it's 'Employee (Names)'. Select Sort by Meta Field and the field of your choice, in our case it is 'Last Name (text)' with Sort order "Ascending" i.e. alphabetical.

  4. Switch to Advanced tab and scroll down to Custom Data and insert the function snippet below. Replace your field name for "last_name".

// declare the field to group by
public function get_variables(): array
    {
        $post_ids = $this->get_default_variables()["_card"]['post_ids'] ?? [];
        $grouped_items = [];

        foreach ($post_ids as $post_id) {
            $last_name = get_field('last_name', $post_id);

            $first_letter = mb_strlen($last_name) > 0
            ? mb_substr($last_name, 0, 1, "UTF-8")
            : "";

        
            if (! key_exists($first_letter, $grouped_items)) {
                $grouped_items[$first_letter] = [];
            }

            
            $grouped_items[$first_letter][] = $post_id;
        }

        return [
            "grouped_items" => $grouped_items,
        ];
    }
//
  1. Switch to the Template tab, and copy the default template into the Custom template field below it.

Modifying from line 5 -> 8.

// Card template example
<acf-card-684a9281e1bf1 class="{{ _card.classes }}acf-card acf-card--id--{{ _card.id }}">

    {% if _card.post_ids %}
        <div class="acf-card__items">
            {% for first_letter, post_ids in grouped_items %}
            {{ first_letter }}
            <hr>
                {% for post_id in post_ids %}
                    [avf_view view-id="{{ _card.view_id }}" object-id="{{ post_id }}"]
                {% endfor %}
            {% endfor %}
        </div>
    {% else %}
        <div class="acf-card__no-posts-message">
            {{ _card.no_posts_found_message }}
        </div>
    {% endif %}


</acf-card-684a9281e1bf1>
  1. Publish or Save your Card and copy the shortcode into place.

Remember to fill out some information and create your posts.

PreviousCustom Ajax & Rest API (Pro)NextDisplay Custom Post Type (CPT) on another post

Last updated 3 days ago

Was this helpful?