This is really more of a php/html question as this problem comes up outside of WordPress. What you want to do is check the current meta name against the previous one, and if it’s different, then you echo the header. You’re already pulling all the posts you need in one go, so it’s something like:
$currentGroup = "";
foreach ($posts as $post) {
setup_postdata($post);
$grouping = get_field('grouping');
if ($grouping != $currentGroup) {
if ($currentGroup != "") {
echo "</ul>";
}
echo "<h2>" . $grouping . "</h2><ul>";
}
$currentGroup = $grouping;
echo "<li><a href="" . the_permalink() . "">" . the_title() . "</a></li>";
}
Cheers.
Related Posts:
- Using a custom WP_Query with get_template_part loop
- How to make WP_Query ‘post__in’ accept an array?
- Is it possible to orderby multiple meta_keys when using meta_value_num?
- Is it possible to wrap Geo Location search around WP_Query?
- WordPress 4.9.5 PHP intermittent warning trim() expects parameter 1 to be string, array given
- Merging multiple wp_query objects
- Query causing load because of SQL_CALC_FOUND_ROWS post counting?
- Using custom meta_query with relation not working as expected
- Nested query inside Logical operator OR not working in meta_query
- SQL Statement generated by WP_Query not producing expected results
- Only show first post to match certain criteria within WP_Query
- What is an efficient way to query based on post_meta?
- Best practice for multiple queries on page
- How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
- Pagination wont work with search results template [duplicate]
- Exclude post on loop by multiple meta key value
- How to find out what “Blog pages show at most” is set to [duplicate]
- difference between $wp_the_query and $wp_query? & getting the values properly [duplicate]
- Sort by meta key on archive page
- Ignore/Skip default value on orderby menu_order?
- WP_Query order by date in meta_value
- WP_Query pagination not working in admin area
- Custom Field sort not working (WP 3.8.1)
- Get post ID’s from one query and exclude from another
- What are the differences between “Latest Posts” and “Static Page”?
- Retrieve or Query Pages by ID
- WordPress Query is taking more then 20 second and stuck on creating index
- Modifying raw $_POST data early in the lifecycle
- Searching through different categories on different pages code is not working
- What’s the point of the query_vars filter?
- Meta query with order by another custom field
- Get the Plugin Which Triggered a MySql Query in WordPress?
- Difficulty with rewrite rules
- Show selected images on top in Media Manager
- Get Category Archive Template Name Dynamically
- How to limit WP_Query to one result on the loop?
- WP User Query get all authors with last name starting with specific letter
- Complex date range with WP_Query and BETWEEN
- Attachment changing page’s permalink – $post not resetting?
- Is there a way to include a post multiple times in the same query result?
- showing all search result in one template
- List custom taxonomy terms sharing posts with a term from a second custom taxonomy
- How can I filter by slug depending on language selection
- WP Query Args – Title or Taxonomy Value
- Ajax WP_Query reutrns no results on author.php
- Large AND OR query timing out
- WP_query wp_posts_counts returns only count of admin and not users
- Categorising search results based on Custom Fields
- How to query for all posts that have a particular meta key?
- Custom taxonomy.php not working
- Ajax future single post query doesn’t work when NOT logged in
- Conditional sorting with variable using WP Query
- Get multiple users with meta value in one query and populate WP_User class
- Advanced WP_Query with meta_query, orderby?
- WP_Query calls all posts?
- WP_Query Taxonomy categories filtering
- WP_Query: apply an SQL function to meta fileld value
- The best way to store variable across different widgets
- Assign a minimum result count for WooCommerce query shortcodes?
- How to search for meta_query LIKE or tax_query LIKE and grab these posts on search results?
- How to create dynamic value in WP_Query meta_query
- WPQuery calling specific posts problem
- Retrieve data from wordpress db via input and put those result in dropdown
- WP_Query with multiple orderby NOT working with ASC and DESC, what’s wrong?
- WP_Query will not display draft posts
- All Posts Have The Same Comments
- Trying to get property of non-object in: $wp_query
- I am officially missing something about transient posts
- Register Taxonomy – What is `query_var`?
- Get Post ID as a separate RSS feed item
- Change post query onclick
- how to retrieve WP_Query without ordering by date [duplicate]
- Query post and sort by custom value calculated with custom field on cpt
- Block internal search queries with pre_get_posts and regex rules
- WP_Query() not working as expected
- How to sort store location by specific category order in WP store locations
- Is it possible to give a classname to specific comments in the WordPress admin?
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- Trouble with serialized metadata
- Is there documentation for objects returned by WP_Query?
- Custom post type loop error: Trying to get property of non-object
- Search Field that allows options of “Match any keyword” or “Match all keywords”
- WP_Query for posts that have postmeta assigned to a taxonomy
- Getting the last X posts, but in ascending order of time
- How to fix pagination for custom loops on front-page.php
- Show more than one post on the main page excluding certain category
- How to make a wordpress loop file that displays posts based on certain conditions
- WP_Query with MetaQuery issue
- Custom loop – Isolating post meta output depending on current query taxonomy terms
- How to sort a WP_Query array by post_name after an array_merge();
- Comment count same for every post in homepage WP_Query
- How do I display posts with specific value in a custom field into my loop?
- Orderby Meta Value and Query from Meta Query
- WP Query to order posts by multiple meta fields
- Order by multiple meta keys on wordpress
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- WP_Query breaks pagination
- wpdb LIKE request shows all database data
- How can I get taxonomy term name using term slug & post ID using build in WordPress function or class?
- Why does wp_query only display the most recent post when using order ASC?