How to Order Posts by a Custom Field in WordPress

Ordering posts by a custom field in WordPress can significantly enhance your site’s functionality, especially when you're dealing with complex content structures. Whether you're running a blog, an e-commerce site, or a portfolio, custom post ordering allows you to showcase your content in a way that better meets your visitors' needs. You can use the following code to sort posts by a custom field using the "pre_get_posts" hook:

function themesdna_order_posts_by_custom_field($query) {
    if (!is_admin() && $query->is_main_query()) {
        if ($query->is_home() || $query->is_search() || $query->is_archive()) { // Adjust conditions based on where you want this to apply
            $query->set('meta_key', 'your_custom_field_key'); // Replace with your custom field key
            $query->set('orderby', 'meta_value'); // Or 'meta_value_num' for numerical values
            $query->set('order', 'ASC'); // or 'DESC'
        }
    }
}
add_action('pre_get_posts', 'themesdna_order_posts_by_custom_field');

Note:
meta_key : Replace 'your_custom_field_key' with the actual key of the custom field you want to order by.
orderby : If your custom field contains text, use 'meta_value'. If it contains numbers (e.g., prices or ratings), use 'meta_value_num' to ensure proper numerical sorting.
order : Set this to 'ASC' for ascending order (lowest to highest) or 'DESC' for descending order (highest to lowest).

Example:
Suppose you have a custom field (meta key) called "event_date", and you want to order posts by that field. You can do this by adding the following code to your theme's functions.php file:

function themesdna_order_posts_by_custom_field($query) {
    if (!is_admin() && $query->is_main_query()) {
        if ($query->is_home() || $query->is_search() || $query->is_archive()) {
            $query->set('meta_key', 'event_date');
            $query->set('orderby', 'meta_value');
            $query->set('order', 'ASC');
        }
    }
}
add_action('pre_get_posts', 'themesdna_order_posts_by_custom_field');

Our WordPress Themes

Below are some of our premium WordPress themes. View all our free and premium WordPress themes →
PowerWP PRO WordPress Theme

PowerWP PRO

If you like PowerWP free WordPress Theme, you will love the premium version. PowerWP PRO is an easy to use,...

$25.00
GridHub PRO WordPress Theme

GridHub PRO

If you have tried GridHub Free WordPress Theme, then you will love the premium version. GridHub PRO version has color...

$25.00
ElegantWP PRO WordPress Theme

ElegantWP PRO

If you like ElegantWP free WordPress Theme, you will love the premium version. ElegantWP PRO is very user friendly, feature-rich,...

$25.00
FreshWP PRO WordPress Theme

FreshWP PRO

If you like FreshWP free WordPress Theme, you will love the premium version. FreshWP PRO is very user friendly, feature-rich,...

$25.00
GridNext PRO WordPress Theme

GridNext PRO

If you have used GridNext Free WordPress Theme, then you will love the premium version. GridNext PRO version has color...

$25.00
TidyMag PRO WordPress Theme

TidyMag PRO

If you like TidyMag free WordPress Theme, you will love the premium version. TidyMag PRO is easy to use, feature-rich,...

$25.00
WriteMag PRO WordPress Theme

WriteMag PRO

WriteMag PRO is improved and feature-rich version of WriteMag Free WordPress Theme. When compare with the WriteMag free version, PRO...

$25.00
HotWP PRO WordPress Theme

HotWP PRO

If you like HotWP free WordPress Theme, you will love the HotWP premium version. HotWP PRO is easy to use,...

$25.00

Can't you choose a single theme? Purchase All Themes for $75.

Save money with our low, one-time price for access to all of our 56 WordPress themes.