Advanced Custom Fields: Sorting Posts Order by ACF Custom Field, Then by Date

Sorting posts order by ACF custom field in WordPress may be extremely useful for prioritizing specific content based on metadata. Advanced Custom Fields (ACF) allows you to store custom data in posts and dynamically order them. In this article, we’ll look at how to sort posts order by custom field first, and then by post date.

Suggested Read: Extra Fees or Additional Charges in WooCommerce

Why should I order by ACF field?

Using ACF fields for ordering is useful when you have:

  • Custom ranking fields (such as priority, rating, and featured level).
  • Event dates, where upcoming events should be listed first.
  • When sorting products or real estate listings using price fields.
  • Any custom numeric or textual sorting.

By default, WordPress arranges posts by their publishing date; but with ACF, we can set a custom order logic.

Fast And Secure Hosting For WordPress

Experience lightning-fast speed, rock-solid security, and world-class support tailored for WordPress. Simply better hosting.

WordPress Query: Order by ACF Custom Field and Then by Date

The following snippet sorts posts first order by a custom field (e.g., priority) and then by the post date in descending order:

$args = array(
    'post_type'      => 'post', // Change to your post type
    'posts_per_page' => -1, // Fetch all posts
    'meta_key'       => 'priority', // ACF custom field name
    'orderby'        => array(
        'meta_value' => 'DESC', // Order by custom field
        'date'       => 'DESC', // Order by date if custom field values are equal
    ),
    'order'          => 'DESC',
);

$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        the_title('<h2>', '</h2>');
    endwhile;
    wp_reset_postdata();
endif;

Explanation:

  • meta_key: Specifies which ACF field is used for sorting.
  • orderby:
    • meta_value: Orders posts by the ACF field value.
    • date: If two posts have the same field value, they are sorted by date.
  • order: DESC means highest values first (change to ASC for lowest first).

Also Read: How to Setup WooCommerce Shipping Zones based on State-Wise PIN Codes

Sorting Numeric ACF Fields Properly

If your ACF field is storing numeric values (e.g., prices, rankings), you should use meta_value_num instead of meta_value. This ensures numbers are sorted correctly instead of alphabetically.

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
    'meta_key'       => 'priority',
    'orderby'        => array(
        'meta_value_num' => 'DESC', // Numeric sorting
        'date'           => 'DESC',
    ),
    'order'          => 'DESC',
);
Looking for a WordPress Developer?

Are you in need of a skilled WordPress developer to bring your website vision to life?
Look no further! Whether you need custom themes, plugin development, site optimization, or ongoing support, I offer expert WordPress development services to suit your needs.

Sorting Events by ACF Date Field

For events, where you want to list upcoming ones first, use a date field and compare it to today’s date:

$args = array(
    'post_type'      => 'events',
    'posts_per_page' => -1,
    'meta_key'       => 'event_date',
    'orderby'        => array(
        'meta_value' => 'ASC', // Oldest events first
        'date'       => 'DESC',
    ),
    'order'          => 'ASC',
    'meta_query'     => array(
        array(
            'key'     => 'event_date',
            'value'   => date('Ymd'),
            'compare' => '>=',
            'type'    => 'NUMERIC',
        )
    ),
);

Explanation:

  • Filters out past events (compare => '>=' ensures only future dates are considered).
  • Orders by event_date in ascending order so that the nearest event appears first.
  • Uses meta_value for date comparison.

Using Pre_get_posts for Global Sorting

If you want to modify the default order for all queries (e.g., blog pages, archives), use pre_get_posts instead:

function custom_query_order( $query ) {
    if (!is_admin() && $query->is_main_query()) {
        if ($query->is_post_type_archive('post')) {
            $query->set('meta_key', 'priority');
            $query->set('orderby', array(
                'meta_value_num' => 'DESC',
                'date'           => 'DESC',
            ));
            $query->set('order', 'DESC');
        }
    }
}
add_action('pre_get_posts', 'custom_query_order');

This will apply the ordering rule to all post queries automatically.

WooCommerce Plugin: WooBooster Additional Charges for WooCommerce

Conclusion

Sorting WordPress posts order by an ACF field gives you powerful control over content ranking. Whether it’s prioritizing featured posts, listing upcoming events, or sorting by price, the right WP_Query approach makes it possible.

Key Takeaways:

  • Use meta_value for text-based fields.
  • Use meta_value_num for numeric fields.
  • Use pre_get_posts to apply sorting globally.
  • Use meta_query to filter specific dates (for event-based sorting).

Thanks for reading 🙏, I hope you will get this code snippet helpful for your project.

Would you like help customizing it further? Contact Us

FAQs

1. Can I order posts by multiple ACF fields?

Yes! You can sort posts by multiple ACF fields by modifying the orderby parameter. For example, sorting by priority first and event_date second:

‘orderby’ => array(
‘meta_value_num’ => ‘DESC’, // Sorting by priority
‘meta_value’ => ‘ASC’, // Sorting by event date
)

2. What happens if some posts don’t have a value for the custom field?

By default, posts without a custom field value may appear at the bottom or may not be included at all. To include them, use a meta_query like this:

‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘key’ => ‘priority’,
‘compare’ => ‘EXISTS’,
),
array(
‘key’ => ‘priority’,
‘compare’ => ‘NOT EXISTS’,
),
),

3. How can I sort WooCommerce products by a custom field using ACF?

If you’re using WooCommerce and want to sort products by a custom field (like custom_price or featured_level), modify the query like this:

$args = array(
‘post_type’ => ‘product’,
‘posts_per_page’ => -1,
‘meta_key’ => ‘custom_price’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘ASC’,
);
$query = new WP_Query($args);

4. Can I order posts by an ACF field in a category archive?

Yes! Use pre_get_posts and check if it’s a category archive like this:

function sort_category_posts($query) {
if (!is_admin() && $query->is_main_query() && is_category()) {
$query->set(‘meta_key’, ‘priority’);
$query->set(‘orderby’, array(
‘meta_value_num’ => ‘DESC’,
‘date’ => ‘DESC’,
));
}
}
add_action(‘pre_get_posts’, ‘sort_category_posts’);

Leave a Reply

Your email address will not be published. Required fields are marked *

Select your currency