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

Adding an orderby filter, casting postmeta with multiple keys

Got the idea from this post to change my query : How to “orderby” the first array in a meta_query that uses multiply keys?

I moved the state query into the larger array of queries, freeing the ‘meta_key’ value to be assigned to event date

$args = array(
    'post_type' => 'tf_events',         
    'meta_query' => array( $relation, $date_query, $date_query2, $state_query ),
    'posts_per_page' => $limit, 
    'meta_key' => 'tf_events_startdate',
    'tax_query' => array( 'relation' => 'AND', $type_query, $host_query ),              
    'order' => 'ASC'
    );

add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
    function my_posts_orderby_date( $orderby, $query ) {
        global $wpdb;  
        return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );
    }

Related Posts:

  1. Excluding posts by meta, and also keeping posts without the meta
  2. Customising the default wordpress search functionality
  3. How to hook a filter to catch get_post_meta when alternate a custom field output?
  4. How to use update_{$meta_type}_metadata filter to modify meta value
  5. What is the most efficient way of querying posts based on visits and date for current day?
  6. How can I display image metadata?
  7. Where is the proper place to add a filter for pre_get_table_charset?
  8. WPDB Multiple site’s posts and get featured images
  9. Possible to filter the posts or categories that XML-RPC users see in their mobile application?
  10. How to filter into post meta data before saving
  11. Filter custom post types in archive
  12. Explanation for remove_filter used in the below code [closed]
  13. Filter list by a unique meta value dilemma
  14. How to remove get_post_metadata using remove_filter inside a class?
  15. Sorting and limitation with pre_get_posts
  16. Filter Posts by current Month
  17. Get .subsubsub count of post per status queried using pre_get_posts
  18. Custom wp_query time filter on meta_value
  19. Is it possible to apply filter to meta key value when querying posts?
  20. Run posts_where and posts_join only on the main query
  21. WordPress query posts with multiple post_meta data
  22. Filter for author list in gutenberg core editor
  23. Filter posts by meta data using custom query
  24. Want to use wp_get_current_user() in query filter
  25. Filter authors on meta value
  26. WP_Query filter by custom meta
  27. List all categories that have results in a query
  28. Display posts from today and future in Elementor ‘posts widget’
  29. Having issue with dynamic data within restrict_manage_posts function
  30. Displaying posts on a page that have been assigned a value in a database
  31. Add option to query string before get_posts() is called on archive.php
  32. Filter on the day of the week from timestamp
  33. Admin view: filter posts by whether they have specific taxonomy attached
  34. Use $wpdb->get_results with filter based on array
  35. Restrict access to custom post type and filter from every query
  36. How to add more than one custom metadata as filter on the post list page?
  37. Hide H1 Title using the_title filter
  38. Retrieve post data via WPDB class
  39. How to create a list of terms who’s posts also have a predefined external term?
  40. Query Multiple Filters, one with Meta
  41. How to use parse_query() to add an additional filter based on content to page search?
  42. WordPress Gutenberg core/heading block (wp-block-heading)
  43. How to insert a value to decimal type field using wpdb->prepare?
  44. How to redirect a unique link based on login status
  45. add_filter( ‘the_title’ gets through this if statement twice
  46. Changing the category for existing Gutenberg blocks
  47. Does this archive template part look like its making too many calls to the db?
  48. Filter Post Title without affecting screen-reader-text
  49. How to modify image caption in posts?
  50. Extend Nonce Lifetime for Specific Nonce Creation
  51. Custom sort orderby question
  52. get_post() with filters applied
  53. Hook to change the site URL
  54. How can I tranlslate post date in italian?
  55. Run `wp_insert_post_data` on all posts
  56. Insert NULL value using prepare()
  57. Replace a specific URL on all apperances on the Website (Maybe a filter?)
  58. Use alternative front page if cookie is set
  59. “The editor has encountered an unexpected error” After add defer tag to java script
  60. Show uploaded pdf files dynamically and filter by month name
  61. Any way to use FETCH_KEY_PAIR with $wpdb?
  62. I want to add string array to this function to filter displayed option in drpodown [closed]
  63. How do I check if I can use the allowed_block_types filter?
  64. Modify WordPress Page Title ()
  65. Add an attribute to the root from functions.php with add_filter
  66. Would like to use hook or filters to customize email templates
  67. Ajax filter – show all results that contain at least one filter
  68. How to securely controlling data without keeping it in postmeta?
  69. Removing menus from users other than the administrator
  70. Filter in Custom post type to find the parent post
  71. How to get inserted row IDs for bulk/batch insert with wpdb query?
  72. “BS_” rows in postmeta table
  73. Delete oldest wordpress post (SQL query)
  74. how to unescape wordpress output
  75. Having wp-admin on different domain
  76. Override wordpress audio playlist
  77. Use a functions in functions.php to remove a string for template theme
  78. Drop down filter sort posts by latest, last 7 days and monthly
  79. Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
  80. Hook to add content after date in post?
  81. Function not returning correct value for found_posts filter
  82. How can I pass instance via apply_filters as a parameter?
  83. How can I limit access to uploaded media depending on the logged in user’s user role?
  84. Create different flavours of excerpt
  85. how to use substr with get_the_content()
  86. Setting the page title in a plugin, but not outside my plugin
  87. add_filter doesn’t return false?
  88. AJAX Post Filter
  89. How to track a particular page in order to address the loading speed issue
  90. How to add lazy field in content endpoint using Gutenberg blocks
  91. Is it possible to assign a css id to a row in plugins list table?
  92. Mystery line break
  93. How to bridge the gap between dynamic back-end data and front-end output?
  94. How to loop year & month condition together to filter correctly via AJAX?
  95. Can’t understand apply_filter logic
  96. Consult array data in REST API Filter
  97. How to add a blog filter bar without paying money. (example inside)
  98. Database & Post Search
  99. login_url filter creates permanent wp-admin -> wp-login redirection loop
  100. WordPress add_filter to post_date
Categories filters Tags filters, post-meta, query, wpdb
Retrieve page content and pass to PHP?
My custom query not detecting the correct category

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