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. SQL query based on two different custom field values
  30. Custom field metabox not showing in back-end
  31. Post meta data not showing in frontend, until hitting ‘update’ button
  32. (woocommerce) How to get product id form order id & then retrieve custom meta linked to product?
  33. searching by keywords in post’s metas or pagination links problem
  34. Add custom field information to source meta data
  35. How do I update custom field post meta in frontend post template?
  36. Update custom field on page specific to logged in user
  37. Query multiple meta values at the same time :
  38. How to Find List of Available Custom Fields for Theme?
  39. Save, update, get and sanitize post meta as HTML not plain
  40. How do I display posts ordered by a date custom field?
  41. WP_Query sort by meta_value_num or date
  42. Create Shortcode shows only posts with custom_field meta
  43. update meta field value after
  44. Create Meta boxes dynamically
  45. Custom Fields Not Working In Footer
  46. ACF plugin and field update
  47. Saved Post Meta Array Returns as String
  48. meta_query ‘compare’ => ‘!=’ not working
  49. Query Multiple meta
  50. Delete custom meta
  51. Query Posts based on custom field value
  52. Problem saving meta data
  53. How to properly get popular Posts by multiple Values
  54. Check if value exists before saving
  55. Echo out custom fields in comments
  56. Get meta value when the page is a blog archive
  57. Search one custom field?
  58. Can’t save meta field value if the title not set
  59. Create custom query for search?
  60. Display Data From This Custom Media Upload Meta Box?
  61. Save values generated via API as custom meta fields
  62. Query postmeta values, and return multiple post_titles for common meta value
  63. Filter query based on date in custom field
  64. When post is updated, custom metadata in text area field is overwritten
  65. Thirtieth century date for a post
  66. How to display custom post type ordered by a custom field date
  67. change attachment custom field onChange event
  68. Adding custom fields to images
  69. How to save dynamically generated value in post type?
  70. wordpress simple post multi rating with post_meta and user_meta
  71. Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
  72. Query Posts Via WordPress URL
  73. wp_postmeta are updated for only one page
  74. update_post_meta not working in action hook
  75. how to show this part only in the single post page?
  76. Custom Field: Display only if a specific key is selected outside the loop
  77. WordPress archives by custom field and date
  78. Meta_query not filtering posts
  79. Order by custom field in query multiple
  80. Complex WP_Query Using Post Date And Post Meta
  81. Display meta data from a custom field within plugin Category Grid View Gallery
  82. When editing a post with a custom meta box the values aren’t displaying correctly
  83. Display posts where date field matches current month?
  84. Hard Define Custom Field Value
  85. Querying meta values within an array
  86. Custom fields / meta box output
  87. If post custom meta data is not set, show normal state
  88. add_post_meta insert null value into Database
  89. Make Custom Fields Public in JSON – API
  90. How do i output images from URL’s added to the same custom field key
  91. Retrieving custom field as shortcode
  92. Colecting values from custom field checkboxes and displaying them in the post
  93. Build Array from Input Fields question
  94. How do delete a meta key?
  95. Filter Query Post by Custom Fields(by date)
  96. Can’t sort custom column on user.php by number / meta_value_num?
  97. custom field with links
  98. add multiple values (array) to post meta_input
  99. Order by a meta field in query loop
  100. Dynamic background image used in css after selector
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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