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

Sort by meta key within same day

So here is a quick solution – with a bit of searching over SO and query analyzing : Keep the main query, but add a special filter for ordering.

The filter (in function.php) :

function my_posts_orderby( $orderby ) {
  $orderby = 'DATE(wp_posts.post_date) DESC, wp_postmeta.meta_value DESC';
  return $orderby;
}

Note the DATE() mysql function that takes the date out of the full time field.

And then, before the loop, run the query thus :

<?php add_filter( 'posts_orderby', 'my_posts_orderby' ); ?>

<?php $the_query = new WP_Query( array( 'meta_key' => '_my_meta_field' ) ); ?>

Wich means, call the filter, and then create a query that includes the custom meta field so that it can be sorted.

Then, after the loop, don’t forget to call

<?php remove_filter( 'posts_orderby', 'posts_orderby' ); ?>

failling that no other query will ever work (including displaying a single post)

And that works.

Related Posts:

  1. Order by optional meta key?
  2. Filtering by multiple conditions in the loop
  3. Orderby two meta fields not working
  4. How to only display posts whose meta_value field is not empty?
  5. Can wp_query return posts meta in a single request?
  6. order by numeric value for meta value
  7. Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
  8. How do I query for posts by partial meta key?
  9. Use REGEXP in WP_Query meta_query key
  10. WP_Query orderby post__in remains ineffective in the Loop [closed]
  11. WP_Query orderby date not working
  12. WordPress retrieving meta data for all custom post types in list view
  13. Sorting: custom query with orderby meta_value_num THEN by title
  14. Order by DESC, ASC in custom WP_Query
  15. WP_Query order by multiple meta keys & fields
  16. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  17. Reduce or prevent calling of update_meta_cache
  18. Secondary Sort (fallback) for WP_Query
  19. Is there a way to extend WP_query so Custom Post Types can have properties?
  20. Query Posts in a Predefined Order
  21. How to get order of posts?
  22. Order posts by ID in the given order
  23. What is the most efficient way of querying posts based on visits and date for current day?
  24. Custom WP_Query order by post_meta and (author) user_meta
  25. Sorting posts by custom date fields (non standard date format)
  26. Getting attachments by meta value
  27. WP_Query displaying ALL posts
  28. meta_query where value is equal to given value
  29. How to order posts tag by tag?
  30. Query Custom Meta Value with Increment
  31. How do I order pages and categories by ID or name in the same query?
  32. WP-CLI How to generate a list of posts with corresponding meta values
  33. WP_Query ordered by custom field that is a date string?
  34. WP_Query sort by comment meta data
  35. What is an efficient way to query based on post_meta?
  36. Order posts by tags count?
  37. Merge 2 args in one WP_Query and order it by date
  38. WP_Query not working as expected for attachments and custom meta_query
  39. Orderby = none not working [duplicate]
  40. How to count post meta key values for all posts in database
  41. WP_query ‘orderby=none’ Problem
  42. Order posts by date and then by custom field
  43. How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
  44. Group posts by meta_key
  45. How should I use posts_where to change meta_value from a string to integer?
  46. Revolution Slider Orderby Two Custom Fields
  47. How can I create a WP_Query that returns posts where one meta_value
  48. Order posts ascending with number in title
  49. List taxonomy terms plus their latest post ordered by post date
  50. WordPress altering my custom query, How to fix it?
  51. WP Meta Query for some meta (array) values
  52. Order by the results of a function with WP_query
  53. WP Query post meta value
  54. Would this post meta be better added to the post table rather than post_meta table
  55. Custom URl parameter
  56. query posts in functions.php and update a field
  57. Compile meta values from custom loop into array and then calculate sum total
  58. How to order posts in wp_query by a meta_value of the corresponding author
  59. How to get sum of meta_values of a meta_key in wp_query according to conditions
  60. how to fire join query with post_meta
  61. Order by menu structure
  62. Best approach to create Hot and Trending sections
  63. WP_Query, custom sort and custom filter
  64. WP_Query order by date in meta_value
  65. How to use order RAND() on WordPress?
  66. Order posts by meta value and Date
  67. Show all parents and children in custom post type in right order
  68. Efficient way to update multiple post meta
  69. Ordering by meta_value AND date NOT WORKING with wp_query
  70. WP_Query orderby breaks when using AJAX?
  71. Order WP Query posts by custom order calculated from post meta values
  72. How to make orderby ‘meta_value_num’ OPTIONAL?
  73. Custom Post order for homepage
  74. Meta query with order by another custom field
  75. Change default ordering of taxonomy terms – pre_get_terms
  76. Query Posts depends on custom field inside repeater field using acf
  77. WordPress WP_Query orderby being overwritten
  78. WP_Query Order by Specific Post ID First
  79. Order Posts by meta value AND published date
  80. How can I pick a single post from the latest 3?
  81. Wp query orderby ‘title’ doesn’t work
  82. Query post with meta_query where date is not in future
  83. Minimising Database Queries when using Advanced Custom Fields
  84. How can I order Wp_Query hierarchically?
  85. Get meta info related to current post
  86. Select from wp_post and multiple meta_value from wp_postmeta
  87. How do I sort posts with multiple pages
  88. Pull post meta with post_query?
  89. Sorting posts DESC based on the number of comments using WP_Query
  90. Order WP_Query by multiple fields, subtracting them from one another
  91. Complex Orderby Parameters: How to query with multiple orderby parameters using meta_value_num?
  92. pre_get_posts order by not working
  93. Group WP_Query by meta_key date
  94. WP_query posts closest to todays date
  95. exclude posts with a specific custom field and value
  96. (Solved) WP_Query ($ args) -> How to sort letters and numbers within the same array
  97. Inserting serialized value into wp_postmeta using update_post_meta
  98. Orderby ASC changes to DESC in WP_Query
  99. SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
  100. Order By table field comment_status in WordPress > 4.0
Categories wp-query Tags order, post-meta, wp-query
post_name is not stored until post is published… where should I store my slug until it goes live then?
Delete custom meta

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