Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Ordering posts by custom field named “date” in backend

The main issue in your code is this part: $query->set('orderby','meta_value_date'); whereby you should use meta_value and not meta_value_date. (There is meta_value_num, but that’s not suitable for used with sorting by dates.)

Then make sure the meta_type is set to DATE (which yes, you already done it correctly), and use the right date format — 10132020 is really not a good date and MySQL/MariaDB also won’t be able to cast such values to a valid date — WordPress/WP_Query instructs MySQL/MariaDB to cast the value as a date when the meta type is set to date.

And in reply to (your comment): “But is there any solution without changing the format and ordering the result query with the chosen format mmddYYYY?“, sorry, but I don’t think there’s an easy way to do so.

And the better practice is use the proper format when saving the metadata and not when retrieving it. For example, you could use the ISO 8601 date format which is also used by current_time( 'mysql' ) in WordPress: Y-m-d — but that’s with PHP; with the jQuery Datepicker widget, you would use yy-mm-dd or $.datepicker.ISO_8601 which then gives a date like 2020-10-14 for October 14th 2020.

So correct the date format and use meta_value with the orderby parameter, and the sorting would work as expected.

Other issues in your code

  1. In crimes_show_columns(), if you need to access the full post data, use get_post() and not the global call. So for example, you can rewrite the function to:

    function crimes_show_columns( $name, $post_id ) {
    //  $post = get_post( $post_id ); // like this
    //  global $post;                 // not this
    
        switch ( $name ) {
            case 'crimedate':
                echo get_post_meta( $post_id, 'date', true );
                break;
        }
    
        // action hooks do not need anything be returned back
    //  return $row_output; // moreover, $row_output is not defined anywhere..
    }
    add_action( 'manage_posts_custom_column',  'crimes_show_columns', 10, 2 );
    
  2. The correct hook (used for adding a custom sortable column name) is manage_edit-post_sortable_columns and not manage_edit-sortable_columns because on the edit.php?post_type=post admin screen, the screen ID is edit-post; i.e. <name of the PHP file without .php>-<post type>.

Related Posts:

  1. Ordering posts by anniversary using only day and month
  2. Displaying posts with only upcoming dates according their custom field date value
  3. ORDER BY custom field value
  4. Trying to perform complex custom field query with order by set to field value
  5. Is there a way to do multiple ordering on a multiple meta_query?
  6. Order by custom field date with ASC order
  7. How can I query on the year part of a complete date in a custom field?
  8. Sorting posts by multiple values, combined
  9. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  10. Sorting posts by custom fields in meta_query
  11. How to add a new meta key and assign timestamp to posts
  12. How do I have WP_Query match posts based on search parameter OR meta fields? (rather than search parameters AND meta fields)?
  13. Alter main archive, to show posts with meta as last
  14. Change order of posts
  15. Meta query with multiple custom fields for archives page ordering problem
  16. Unique meta_key with array value vs repeated meta_key with single values
  17. WP Query – Is this correct?
  18. Avoiding ACF get_field and returning to core WordPress function
  19. ORDER BY custom field value out of where clause
  20. Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
  21. change order of images attached to post
  22. How to query posts by month based on date custom field?
  23. Group Posts By Custom Meta Value: Date
  24. How to improve my non-unique metadata MySQL entries?
  25. WordPress Admin – Automatically Sort Custom Posts by Custom Field Date Value in d-m-Y Fomat
  26. Order a query by meta_value and then by post_date
  27. Checking if field is set before comparing with meta_query in query_posts?
  28. WP Query Args – search by meta_key or title
  29. Matching Serialized Arrays with meta_query
  30. Custom field metabox not showing in back-end
  31. So much data in postmeta
  32. get_post_meta() unserialize issue – returns boolean(false)
  33. How Can I save multiple records in same meta key?
  34. Up/Down voting system for WordPress
  35. post meta data clearing on autosave
  36. WordPress Search Custom Meta Field Only
  37. Order Custom post type loop by custom field (datepicker)
  38. Loop through two different sets of custom fields
  39. Localized Date Format for Custom Field
  40. Difference between ‘LIKE’ and ‘IN’ in meta queries
  41. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  42. Saving custom image meta fields
  43. Get all meta keys assigned to a post type
  44. Complex WP_User_Query call fails on production server
  45. How to query posts with certain custom meta data, and output Post data
  46. How can I sort posts by the date and a custom meta field?
  47. Custom field not updating when value is empty
  48. Limit the number of acf content when displaying in post loop [closed]
  49. How to add custom metadata text box dropdown to sidebar in Gutenberg editor for all post types
  50. Add post meta fields, when creating a post using WordPress’ REST API
  51. Store array as single, serialized post meta field or many post meta fields?
  52. Custom Field Values not updating unless I click “Update” twice
  53. update_post_meta for repeated custom field [closed]
  54. Large AND OR query timing out
  55. Trying to update_post_meta function
  56. Show values of custom post meta on ‘Add new post’ page?
  57. Custom post meta field effect on the performance on the post
  58. get_posts in meta box dropdown not showing latest posts
  59. filter custom field values $min $max
  60. comment meta_query for keys that aren’t yet set
  61. Looping inside block return
  62. Sortable Custom Columns not sorting correct
  63. how can I show name and value of Custom Fields together?
  64. tax query between operator like
  65. Gravity Forms Update post meta from template [closed]
  66. Best way to achieve multiple links in a post title
  67. Order custom fields alphabetically
  68. How show post only from after custom date
  69. Is it possible to compare the current time with a custom “start” and “end date
  70. How can I modify my meta_query to work with prices that are stored in the database that contain dollar signs and commas?
  71. wordpress multi user question
  72. multiple meta_query and orderby question
  73. Adding Custom Metadata to my Archive/Posts page
  74. Trouble with serialized metadata
  75. Orderby if between two meta fields
  76. Ordering by ACF custom field vaule
  77. Query against multiple locations within single custom post type post
  78. Custom Fields Not Showing (ACF not installed)
  79. SQL query to change the value of a Custom Field
  80. How do I update custom field post meta in frontend post template?
  81. Query multiple meta values at the same time :
  82. How to Find List of Available Custom Fields for Theme?
  83. Create Shortcode shows only posts with custom_field meta
  84. Custom Fields Not Working In Footer
  85. ACF plugin and field update
  86. Search one custom field?
  87. Can’t save meta field value if the title not set
  88. Adding custom fields to images
  89. How to save dynamically generated value in post type?
  90. how to show this part only in the single post page?
  91. Display meta data from a custom field within plugin Category Grid View Gallery
  92. Display posts where date field matches current month?
  93. If post custom meta data is not set, show normal state
  94. Colecting values from custom field checkboxes and displaying them in the post
  95. Build Array from Input Fields question
  96. How do delete a meta key?
  97. custom field with links
  98. Check for custom field value in different post type than current one and do something
  99. How to add new Metadata options (Date, Author, etc.) for Posts?
  100. Saving multiple custom meta box fields
Categories custom-field Tags custom-field, date, meta-query, order, post-meta
How To Load an HTML File As A WordPress Page (With No 301/No Redirect)
Error invalid parameters with REST API

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress