Display Grouped Posts
For Advanced Views Pro users to help you 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 alphabetical order with the Last name in Ascending order, and include a line with first letter of the last name.
Step by step guide
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.
Visit the Advanced Views tab in WordPress backend.
Create your Layout
Click the Add New button to add a new Layout and Enter a Name for your Layout.
Click the Add Field button in the Assign 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.
Click on the Publish button to save and publish your Layout.
Optional — Continue editing your Layout, 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 Post Selection
Use the "Add new" button under 'Assigned to Post Selections' in the right sidebar of your Layout, or Simply go to the Post Selections top tab.
Click the Add New button to add a new Post Selection, enter a Name for your Post Selection.
On the General tab choose Sort by "Meta value", with the Sort by Meta Field Group the one containing the name field, 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.
Switch to Advanced tab and scroll down to Custom Data (Pro) and insert the function snippet below. Replace your field name for "last_name".
// ...
return new class extends Custom_View_Data {
public function get_variables(): array
{
$post_ids = $this->get_default_variables()["_card"]['post_ids'] ?? [];
$grouped_items = [];
foreach ($post_ids as $post_id) {
// declare the field to group by
$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,
];
}
// ...
}Switch to the Template tab, and copy the Default template content 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-layout id="{{ _card.view_id }}" object-id="{{ post_id }}"]
{% endfor %}
{% endfor %}
</div>
{% else %}
<div class="acf-post-selection__no-posts-message">
{{ _card.no_posts_found_message }}
</div>
{% endif %}
</acf-card-684a9281e1bf1>Publish or Save your Post Selection and copy the shortcode into place.
Remember to create your posts and fill out some information in the fields.
Last updated