/ Blog / Tutorials / How to Sort WordPress Posts by Custom Fields

How to Sort WordPress Posts by Custom Fields

Nov 22, 2024

Custom fields in WordPress are like secret ingredients that add flavor to your posts, pages, and custom post types. They allow you to store and display extra information in a structured format, such as dates, prices, ratings, or any other specific data that suits your needs.

However, just having custom fields isn’t enough – you need to be able to manage and sort through them with ease. Whether you have an eCommerce site with product prices or a review site with star ratings, sorting by custom fields can make content management much easier, especially if you manage a large website with an extensive content library.

However, sorting posts by custom fields in WordPress isn’t as straightforward as it sounds. Without the right tools, you might find yourself tangled in the complexities of modifying the WordPress loop or creating custom plugins. These tasks can be time-consuming and prone to errors, turning a simple need into a major headache.

But don’t worry! In this article, we’ll introduce you to an excellent tool that makes ordering posts by custom field data on the WordPress backend a piece of cake. Plus, we’ll guide you on how to tweak the default post order on the WordPress front end to sort by custom field data.

Stick around, and let’s make your content management a whole lot easier!

Enhancing custom field data management with Admin Columns

Admin Columns Homepage

If you’re looking to order, sort, or filter posts by custom field data in the WordPress dashboard, Admin Columns is your best friend. This powerful WordPress data management plugin streamlines how you manage posts in the WordPress admin area, making it easier to handle custom field data without the usual headaches.

Customizing content display in the WordPress dashboard

Admin Columns lets you customize the display of list tables in your WordPress dashboard by adding, removing, and rearranging columns to suit your specific needs.

List table columns overview in Admin Columns.

In just a few clicks, you can create a dashboard that shows exactly what you need where you need it, including custom fields.

Custom field types in Admin Columns

The magic doesn’t stop there. Admin Columns integrates seamlessly with popular custom field plugins like Advanced Custom Fields (ACF), Pods, Meta Box, Toolset, and JetEngine.

This means you can display custom fields created with these plugins as columns in your list tables. Whether you’re managing product prices, event dates, or any other custom data, Admin Columns makes it visible and manageable right from your dashboard.

Example of a list table with custom field types

Check out Admin Columns’ guide on custom fields plugins for more detailed information about the different tool options, their features, and what type of content you can create with them.

Sorting and filtering posts by custom fields

Another great feature of Admin Columns is its ability to sort and filter your website content by custom field data. This means you can organize your posts in a way that makes sense for you, whether by date, price, rating, or any other custom field.

For example, you can easily sort the cars in this custom post view by price, mileage, or year just by clicking on the little arrows in the column name – up for ascending and down for descending.

Sorting a custom field column in ascending order

Besides the column sorting, you can also filter list table data by nearly any field type and a wide array of conditions. Just click on Add Filter and add as many conditions as you want.

Adding multiple custom field filters with Admin Columns

To make your data management process even easier, you can save these filtered segments for future use so that you don’t have to apply the same combination of filters every time you need to use them.

Saving a filter combination

Boosting productivity with inline and bulk editing

Admin Columns doesn’t just stop at managing, sorting, and filtering your custom field data; it also offers powerful inline and bulk editing capabilities. This means you can update your posts, pages, custom post types, and other content directly from the list table.

The inline editing feature allows you to update individual data fields, including custom field data, right from the list table.

Inline editing a custom field with Admin Columns

If you need to make changes to multiple posts or pages at once, Admin Columns’ bulk editor has you covered. You can update unlimited posts, pages, or custom post types in bulk, making large-scale updates quick and painless.

Bulk editing custom fields with Admin Columns

We’ve successfully tested bulk editing on a database with more than 100,000 records, but you can go even further than that.

Another crucial feature for large stores and websites is that Admin Columns allows you to select every single record in your database, not only the ones that show up on a single page, which is no more than 999 records.

WooCommerce limitation on items per page

For more information, check out Admin Columns’ guide on bulk editing in WordPress.

Step-by-step: Sorting posts by custom fields with Admin Columns

Adding a custom field column is easy! Here’s what you need to do:

  1. Go to the list table you want to add to – in our example, the Cars custom post table – and click on Edit columns.

    Edit columns button in the list table
  2. In the Columns settings page, click on + Add Column.

    Adding a new column to a list table
  3. Select the custom field you want to add.

    Choosing a custom field type from Advanced Custom Fields
  4. Adjust settings, such as the ability to export the data from that field, use smart filters, bulk edit, and sort. 

    Adjusting settings for a custom field column
  5. Go back to the list table and click on one of the arrows to sort the information.

    Sorting a custom field column
  6. If you want to filter by this custom field, just click on Add Filter and customize the segmentation.

    Filtering data with a custom field

Ordering posts by custom fields on the WordPress front end

Reordering WordPress posts or custom post types by custom fields on the front end of your site can be incredibly useful. For instance, if you run an events management site, it would make more sense to display events by their start date rather than the date the event was published on your site. This way, your users see the upcoming events first, enhancing their experience and making your site more intuitive.

The easiest way to do this is to use a no-code solution. If you’re looking for inspiration, we cover some of the best options for improving your post ordering and user experience in our guide on the top WordPress search filter plugins. That said, if you’re a bit tech-savvy and love working with code, there is a way to do simple sorting programmatically, as well.

By default, WordPress displays posts in reverse chronological order by publication date. However, you can change this default order by modifying the orderby parameter of your WP_Query class. This allows you to order posts by custom fields, such as event start dates, product prices, or any other custom data you have added to your posts.

Keep in mind that changing the orderby parameter requires some coding knowledge. For example, this is what you’d need to write to reorder posts by event start date:

// Hook our custom query function to the pre_get_posts
add_action('pre_get_posts', function (WP_Query $query) {
    // Make sure we only modify the main query on the homepage
    if (is_admin() || ! $query->is_main_query() || ! $query->is_home()) {
        return;
    }
 
    // Set parameters to modify the query
    $query->set('meta_key', 'start_date');
    $query->set('meta_type', 'DATETIME');
    $query->set('orderby', 'meta_value_num');
    $query->set('order', 'ASC');
    $query->set('suppress_filters', true);
});

And here are the functions of the different parts:

  • pre_get_posts – modifies the current posts query before it’s executed.
  • meta_key – specifies the custom field that we’re using to order our posts; in this case start_date.
  • meta_type – specifies the type of data in the field; in this case DATETIME, changing how the data is sorted (by default, it will be alphabetical, which is problematic for numerical data).
  • orderby – specifies what parameter should be used to order the post data, in this case, meta_value_num
  • meta_value_num – specifies that the data to order posts comes from a numerical custom field. The exact field is specified by the meta_key parameter.
  • order – specifies the order in which data is presented, either ascending (ASC) or descending (DESC).
  • suppress_filters – prevents filters from altering the query.

Keep in mind that reordering your posts or custom post types by custom fields, while useful, is only one step in creating an intuitive user experience on your site. To take this to the next level, a dedicated search filter plugin is the way to go. These plugins enable users to more easily filter your posts and custom post types more easily, helping them find the content most relevant to them more quickly.

Take the next step in custom field data management with Admin Columns

Custom fields are crucial for structured data management in WordPress, enabling you to add and organize extra information like dates, prices, and ratings. Sorting and ordering your posts by custom fields brings significant benefits both on the front end and back end of your site.

On the front end, it enhances user experience by allowing you to organize posts in a more intuitive way. Visitors can easily find the information they’re looking for, improving overall engagement with your site. On the back end, it simplifies data management, making it easy to sort and filter posts by specific criteria. This is particularly useful for segmenting posts for analysis or bulk updates.

Admin Columns is the ultimate tool for custom field data management in WordPress. It offers a seamless way to order, sort, and filter your posts by custom fields, making your content management more efficient and productive.

Ready to take your custom field data management to the next level? Get started with Admin Columns today and transform how you manage your WordPress content!

Get Admin Columns Pro

Effortlessly sort, filter, edit, export, and organize content in the WordPress admin.

$ 79 / year