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
Step by step guide
// ...
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,
];
}
// ...
}Last updated