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

CleanWP PRO

If you like CleanWP free WordPress Theme, you will love the CleanWP PRO version. The premium version of CleanWP WordPress...

$25.00
MagShow PRO WordPress Theme

MagShow PRO

MagShow PRO is the premium version of MagShow free WordPress Theme with more advanced features. MagShow PRO version has 160+...

$25.00
GridBit PRO WordPress Theme

GridBit PRO

Are you looking for a feature-rich version of the GridBit Free WordPress Theme? Then you will fall in love with...

$25.00
PureMag PRO WordPress Theme

PureMag PRO

If you like PureMag free WordPress Theme, you will love the premium version. PureMag PRO is an user friendly, HTML5/CSS3...

$25.00
GridMag PRO WordPress Theme

GridMag PRO

If you like GridMag free WordPress Theme, you will love the premium version. GridMag PRO is an improved version of...

$25.00
Simple Grid PRO WordPress Theme

Simple Grid PRO

You'll be pleased with the Simple Grid PRO WordPress theme if you have tried the free version. It is a...

$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.