How to group Posts by field
In Advanced Views, the Post Selection interface supports sorting but does not provide built-in grouping options. To display results grouped by a specific field, you'll need to customize the Selection Controller.
This step-by-step guide demonstrates how to display Event custom post type items grouped by a date field.
1. Preparations
First, create your Field Group with the fields; in this case, it's Event Date (date) assigned to a Custom Post Type (CPT) called Events.
Visit the Advanced Views tab in the WordPress backend.
2. Layout creation
Click the Add New Layout button to add a new Layout, then 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 that should be included. In our case, we're only adding two fields: Title with Link and Date.

Click on the Publish button to save and publish your Layout.
3. Post Selection creation
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 Post Selection button to add a new Post Selection, then enter a Name for your item.
On the General tab choose Item Layout 'event', select Post Type Event, for Sort by "Meta value", with the Sort by Meta Field Group the one containing the date field, in our case it's 'Date(ACF)'. Then,, for Sort by Meta Field, select 'Date (date_picker)' with Sort order "Ascending," i.e. alphabetical (This is important; otherwise, the grouping won't work correctly).

Switch to the Advanced tab and scroll down to Selection Controller and insert the function snippet below. Replace your field name with "date".
Switch to the Template tab, and copy your Default template content into the Custom template field below it.
Modifying from line 4 -> 18.
Publish or save your Post Selection and copy the shortcode into place.
Remember to create your event posts and fill out some information in the fields.

Last updated