How to automatically sort the order of a custom post type in the WordPress admin area

To automatically sort a custom post type by date in the WordPress admin, you can use the pre_get_posts action hook. Here’s a function that you can add to your theme’s functions.php file or a custom plugin:





Explanation:

  • is_admin(): Ensures that the function only runs in the admin area.
  • $query->is_main_query(): Ensures that we are modifying the main query, not any secondary queries.
  • $query->get('post_type') === 'your_custom_post_type': This condition ensures that the sorting is applied only to the specific custom post type you want to target. Replace 'your_custom_post_type' with the actual slug of your custom post type.
  • $query->set( 'orderby', 'date' ): Orders the posts by the date field.
  • $query->set( 'order', 'DESC' ): Sorts the posts in descending order (most recent posts first).

This function will automatically sort the posts of the specified custom post type by date in the WordPress admin area.

More Articles
  • The Complete Accessibility Checklist for WordPress Content Editors

    Maintaining a fully accessible WordPress website is not just a technical responsibility—it’s an ongoing content discipline. If you manage or publish posts, your decisions directly impact whether people using screen readers, keyboard navigation, or other assistive technologies can fully engage with your content. This guide is designed as a practical, repeatable reference for anyone creating…

  • Detailed close-up of a strong shiny metallic chain link against a blurred background, emphasizing security.

    Best practices for adding links to web copy to maximize SEO benefits

    There isn’t a strict rule for the exact number of hyperlinks in body copy, but there are widely accepted best practices based on usability, SEO, and readability. The goal is to include links where they add value, not simply to increase quantity. 🔗 1. General guideline (most common rule of thumb) For standard website pages…

  • website, web design, development, code, programming, marketing, office, business, agency, website, website, web design, web design, web design, web design, web design, agency

    25 Free Website Testing Resources Every Web Developer Should Know

    Modern web development extends far beyond writing functional code. Developers must ensure their websites are secure, accessible, fast, and compliant with modern search and usability standards. Fortunately, there are many powerful free tools available that help identify issues, validate markup, test accessibility, analyze performance, and improve overall site quality. Below is a curated list of…

Skip to content