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 →
FlashWP PRO WordPress Theme

FlashWP PRO

If you like FlashWP free WordPress Theme, you will love the premium version. FlashWP PRO is advanced, more user friendly,...

$25.00
NeatBlog PRO WordPress Theme

NeatBlog PRO

NeatBlog PRO WordPress Theme is the advanced, feature-rich version of the NeatBlog Free WordPress Theme. NeatBlog PRO version has color...

$25.00
CoolWP PRO WordPress Theme

CoolWP PRO

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

$25.00
RapidWP PRO WordPress Theme

RapidWP PRO

Did you try RapidWP Free WordPress Theme?, Then you will love the premium version. RapidWP PRO is an improved, more...

$25.00
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
GridMax PRO WordPress Theme

GridMax PRO

GridMax PRO is a feature-rich, advanced WordPress theme than its free version. If you like GridMax Free WordPress Theme, then...

$25.00
GridFlex PRO WordPress Theme

GridFlex PRO

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

$25.00
TextWP PRO WordPress Theme

TextWP PRO

TextWP PRO is an more improved and advanced version of TextWP Free WordPress Theme. When compare with the TextWP free...

$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 55 WordPress themes.