I would do it the other way around- loop over the alphabet and check all posts for a match with each letter.
$all_posts = new WP_Query(
array(
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
)
);
if( $all_posts->have_posts() ){
foreach( range( 'A', 'Z' ) as $letter ) {
echo '<div class="group_letter"><div class="letter">' . $letter . '</div>';
while( $all_posts->have_posts() ){
$all_posts->the_post();
$title = get_the_title();
$initial = strtoupper( substr( $title, 0, 1 ) );
if( $initial == $letter ){
echo '<div class="post">' . $title . '</div>';
}
}
$all_posts->rewind_posts();
echo '</div>';
}
}
Related Posts:
- Display sorting options dropdown when using WooCommerce product category shortcode
- Drop down+sort blog posts date added/most popular
- What is the most efficient way to search users by their display name?
- Archive listing of posts by publish year (multiple years)
- Sortable admin column for one meta key with three possible meta values
- Order posts randomly, not by date
- Nesting if statements to echo only one string based on what tag was filtered?
- Sort posts by custom fields value using dropdown menu
- How to Sort posts by Vote Count?
- Sort query_posts for Parent Pages to menue order or the count?
- How to sort posts based on the value (number) of a post’s metadata?
- Sort custom meta column by other meta value
- Sort the result of the main Query in shop page by a custom taxonomy
- Sort by page information by Ascending Numbers
- How to keep track of when a vote is cast?
- change the default order of posts only for specific categories
- Order BY Most Liked And Published Between Previous Week Monday And Next Week Monday
- How to tag the main tag? [closed]
- Removing custom sort order from admin page listing
- WordPress sorting posts by date and title using a dropdown
- How to sort (orderby) a query done by a template function before the ‘foreach’ loop?
- Array sorting assistance
- Page 2+ of taxonomy archives does not recognise sort-order from dropdown
- Query category-specific, paginated posts and allow viewer to change sort order
- Sort posts by title, sort array by largest number [duplicate]
- WordPress – show all posts + ones with specific tag
- ISOTOPE – Missing/Invalid Arguement Get Terms
- Posts sortable column not sorting properly for custom field numbers
- category & sub category order issues in wordpress
- Sort author list alphabetically
- What is wrong with my sorting logic?
- Displaying Logged-In User Name in WordPress Menu
- How to use wp_insert_user on WordPress Multisite?
- How do I add a php statement to a jQuery string
- Upload images and attachments from frontend form
- Should I use wp_mail or PHP’s mail? [duplicate]
- Successful or Error Message after running mysql code in functions.php
- Link to file in plugin directory from wordpress template?
- Deregister and Dequeue Stylesheets From a Plugin and Enqueue a Child’s Stylesheet/s
- How to use return in my custom function instead of echo
- Validate Uploaded Image using WordPress’ Built-in Functions?
- $wpdb->flush(); breaks the loop
- Global variable $post returning incorrect object
- how to make separate field in wordpress user_data_
- Proper syntax or method for keeping url in modified isotope / category links
- Gutenberg block – Save PHP function’s output as HTML?
- Custom search results page not working with empty search
- Which hook/action will help me solve my problem?
- Tax query in pre_get_posts not working
- Programmatically inserting post through URL – when including wp-load.php, it’s redirecting to wp-admin/install.php
- curl_exec causes timeout on mod_fcgid
- Accessing WPDB Multidimensional JSON Array w/ Javascript
- Fatal error: Call to a member function query() on a non-object
- How to replace file_get_contents() with a WordPress Filesystem call
- Remove dash from blog title wordpress
- Include text in echoed hyperlink
- set post limit at load more ajax wordpress
- How to use AJAX to call php page with parameter
- My website is not showing Footer section
- WordPress meta_query >= &
- Change header on pages displaying taxonomy items
- Listing titles and custom fields of entries within subcategories
- Must filter functions receive all arguments passed to them?
- Prevent Company Name From Translating
- Displaying Slider in Front Page (home) using Customizr Theme
- Usage of var keyword in the core
- How to add PHP pagination to wordpress
- PHP displaying wrong custom taxonomy images
- Slow site initial load time
- MySQL Query Returns Array () In Shortcode
- array wordpress when get is null
- PHP session not staying alive. headers already sent
- how can I call a function when time expire
- Google CSE Malfunctions via Chrome/Safari on Mobile When Clicking on Either Search Icon/Menu Icon. How to Make Google CSE Default Theme Search
- Can an AJAX callback function access a PHP variable which was defined during initial page load?
- Latest posts feed with a specific post always first
- WordPress Post undefined error with Post Date (New to Wordpres Dev)
- Move Jquery.js to Footer
- Updating the database after parsing CSV document
- Attaching an upload to a user
- Change home_url in searchform.php to go to HTTPS instead of HTTP
- can’t grab PHP $_POST variable
- Loop 1 user randomly
- Wp_mail doesnt work
- Use WP_Query Data In Cookie
- Sort by multiple options in custom field
- Is custom behaviour possible when asynchronously uploading?
- How can I show only if custom field has content [closed]
- Top menu disapears in Category Pages when filtering using ACF
- doubled content
- Adding if statement to content for homepage
- Tables not showing divs and loop/php items
- Keeping the previous get value and add another value when submitted
- Pagination not displaying
- How to add active class dynamically
- Setting WooCommerce currency programmatically
- Masking external links with internal link for member-only
- I’m not able to get access $wpdb in my php file in wordpress
- Submit cf7 form to cfdb as pdf/BLOB [closed]
- Correct way to perform a GET to another PHP file hosted on wordpress