How to Fix WordPress Query Sorting Issues with ACF Custom Fields
Content
If you've ever built a query to display posts ordered by an Advanced Custom Fields (ACF) value, only to find the results are jumbled, out of order, or not sorting at all, you're not alone. This is a common challenge when moving beyond WordPress's default sorting by date or title. This guide will walk you through the most common causes and their solutions.
Why Does This Happen?
WordPress's WP_Query is powerful, but sorting by a custom field requires specific parameters. The data type stored by ACF (string, number, date) often doesn't match the default sorting method used by the database, leading to incorrect results. For example, a number field sorted as a string will order '10' before '2'.
Common Solutions for Sorting ACF Fields
1. Correctly Sorting by a Number Field
If your numeric values are sorting alphabetically (e.g., 1, 10, 2, 21), you must tell WordPress to treat the meta value as a number.
$query = new WP_Query( array(
'post_type' => 'your_post_type',
'posts_per_page' => -1,
'meta_key' => 'your_number_field',
'orderby' => 'meta_value_num', // Use 'meta_value_num' for numbers
'order' => 'DESC'
) );
2. Correctly Sorting by a Date Field
Sorting by a date is a frequent point of confusion. You must ensure the ACF field's return format matches the `meta_type` in your query. A common format is 'Ymd' (e.g., 20231031).
$query = new WP_Query( array(
'post_type' => 'event',
'meta_key' => 'event_date',
'orderby' => 'meta_value', // Order by the meta value
'order' => 'ASC', // Oldest to newest
'meta_type' => 'DATE', // Instruct WordPress on the data type
'meta_query' => array( // Optional: Only get future events
array(
'key' => 'event_date',
'value' => date('Ymd'),
'compare' => '>='
)
)
) );
Key Tip: The meta_type is crucial. For a date stored as 'Ymd', use 'DATE'. For a DateTime field, you would use 'DATETIME'.
3. Preserving the Order from a Post Object or Relationship Field
By default, a query of post IDs from a Post Object or Relationship field will be ordered by the post date. To preserve the manual order your user selected in the dashboard, you must use the `post__in` parameter and set `orderby` to `post__in`.
$selected_post_ids = get_field('your_relationship_field'); // Returns an array of Post IDs
if( $selected_post_ids ) {
$query = new WP_Query( array(
'post_type' => 'any',
'post__in' => $selected_post_ids, // Use the specific IDs
'orderby' => 'post__in', // This preserves the order from the array
'posts_per_page' => -1
) );
}
4. Fixing Pagination and Duplicate Posts
Complex meta_query arrays can sometimes cause pagination issues and duplicate posts. This is often due to the SQL JOIN statements confusing the found posts calculation. The standard fix is to suppress filters.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'post_type' => 'your_post_type',
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'your_field',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'suppress_filters' => true // Can help prevent pagination issues
) );
Debugging Your Query
If your results are still not as expected, debugging is your best friend. Add this code after your query to see the raw SQL command WordPress is executing.
// After your WP_Query call
if( $query ) {
echo '<pre>';
print_r( $query->request ); // Displays the SQL query
echo '</pre>';
}
Examining this query can reveal if the wrong table is being joined or if the ORDER BY clause is not what you intended.
Conclusion
Successfully querying and sorting by ACF custom fields hinges on using the correct combination of orderby, meta_key, and meta_type parameters. By matching the query to the type of data stored in your field, you can reliably display your content in any order you need.
Related Support Threads Support
-
relational field order of elementshttps://wordpress.org/support/topic/relational-field-order-of-elements/
-
Post listing for CPT should have the same sorting as the regular postshttps://wordpress.org/support/topic/post-listing-for-cpt-should-have-the-same-sorting-as-the-regular-posts/
-
Sorting for custom field date time picker is not workinghttps://wordpress.org/support/topic/sorting-for-custom-field-date-time-picker-is-not-working/
-
Custom order/position for the ACF sectionshttps://wordpress.org/support/topic/custom-order-position-for-the-acf-sections/
-
Custom alphabetical artist order code not workinghttps://wordpress.org/support/topic/custom-alphabetical-artist-order-code-not-working/
-
Filtering and ordering by ACFhttps://wordpress.org/support/topic/filtering-and-ordering-by-acf/
-
Admin dashboard posts view – sortable custom field column?https://wordpress.org/support/topic/admin-dashboard-posts-view-sortable-custom-field-column/
-
Alphabetical Orderhttps://wordpress.org/support/topic/alphabetical-order-31/
-
WP_Query Results Issuehttps://wordpress.org/support/topic/wp_query-results-issue/
-
posts_clauses drop ACF get_field functionhttps://wordpress.org/support/topic/posts_clauses-drop-acf-get_field-function/
-
Sort scf repeater backendhttps://wordpress.org/support/topic/sort-scf-repeater-backend/
-
Option [Position] of groups of posts does not work.https://wordpress.org/support/topic/option-position-of-groups-of-posts-does-not-work/
-
Slow sql requestshttps://wordpress.org/support/topic/slow-sql-requests/
-
Conflict with Yoast SEO and Select2https://wordpress.org/support/topic/conflict-with-yoast-seo-and-select2/
-
taxonomy-select in Repeater in grouphttps://wordpress.org/support/topic/taxonomy-select-in-repeater-in-group/
-
REST API – Ordering by ACF Date fieldhttps://wordpress.org/support/topic/rest-api-ordering-by-acf-date-field/
-
Order of post metahttps://wordpress.org/support/topic/order-of-post-meta/
-
ACF Wrong Number Ordering Over 10https://wordpress.org/support/topic/acf-wrong-number-ordering-over-10/
-
duplicated posts in paginationhttps://wordpress.org/support/topic/duplicated-posts-in-pagination-2/
-
Change taxonomy order in admin columnhttps://wordpress.org/support/topic/change-taxonomy-order-in-admin-column/
-
custom query with order by number (range)https://wordpress.org/support/topic/custom-query-with-order-by-number-range/
-
acf post object field conflict with yoast seo pluginhttps://wordpress.org/support/topic/acf-post-object-field-conflict-with-yoast-seo-plugin/
-
How to sort the values of the selected field?https://wordpress.org/support/topic/how-to-sort-the-values-of-the-selected-field/