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

Orderby two meta fields not working

SPECIAL THANKS TO THIS ARTICLE: http://dotnordic.se/sorting-wordpress-posts-by-several-numeric-custom-fields/

$args = array(
  'post_type' => 'property',
  'tax_query' => array(
    array(
      'taxonomy' => 'rc_repl_status',
      'field'    => 'slug',
      'terms'    => 'sold'
    )
  ),
  'posts_per_page' => 100,
  'meta_key' => '_rc_repl_property_year_sold',
  'meta_query'  => array(
    array(
      'key' => '_rc_repl_property_year_sold' // do this to order by two meta field VALUES
    ),
    array(
       'key' => '_rc_repl_property_acres', // do this to order by two meta field VALUES
    ),
   ),
);

// do this to order by two meta field VALUES - This takes generic values and
// can be used with any WP_Query meta_query
function customorderby($orderby) {
  return 'mt1.meta_value DESC, mt2.meta_value+0 DESC';
}

// Now apply your filter. Add it before your query and remove it 
// afterwards to make sure it doesn’t affect subsequent queries.
add_filter('posts_orderby','customorderby');
$query = new WP_Query( $args );
remove_filter('posts_orderby','customorderby');

Related Posts:

  1. Order by optional meta key?
  2. Order By table field comment_status in WordPress > 4.0
  3. echo a specific meta_key queried through a custom post
  4. Filtering by multiple conditions in the loop
  5. Custom order revolution sliders post base slides as inserted in Specific Posts List field
  6. Sort by meta key within same day
  7. How to only display posts whose meta_value field is not empty?
  8. Nested meta_query with multiple relation keys
  9. Can wp_query return posts meta in a single request?
  10. order by numeric value for meta value
  11. numberposts? showposts? posts_per_page?
  12. Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
  13. How do I query for posts by partial meta key?
  14. Use REGEXP in WP_Query meta_query key
  15. WP_Query orderby post__in remains ineffective in the Loop [closed]
  16. WP_Query orderby date not working
  17. WordPress retrieving meta data for all custom post types in list view
  18. Sorting: custom query with orderby meta_value_num THEN by title
  19. Order by DESC, ASC in custom WP_Query
  20. WP_Query order by multiple meta keys & fields
  21. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  22. Reduce or prevent calling of update_meta_cache
  23. Secondary Sort (fallback) for WP_Query
  24. Is there a way to extend WP_query so Custom Post Types can have properties?
  25. Query Posts in a Predefined Order
  26. How to get order of posts?
  27. Order posts by ID in the given order
  28. What is the most efficient way of querying posts based on visits and date for current day?
  29. Custom WP_Query order by post_meta and (author) user_meta
  30. Sorting posts by custom date fields (non standard date format)
  31. Getting attachments by meta value
  32. WP_Query displaying ALL posts
  33. How can I save an array from a random post sequence for later use?
  34. meta_query where value is equal to given value
  35. How to order posts tag by tag?
  36. Query Custom Meta Value with Increment
  37. How do I order pages and categories by ID or name in the same query?
  38. WP-CLI How to generate a list of posts with corresponding meta values
  39. All-in-One Event Calendar: Custom Query – Getting each event Instance [closed]
  40. Is there an action/filter called when WP_Query->query is finished and assigns posts
  41. WP_Query ordered by custom field that is a date string?
  42. WP_Query sort by comment meta data
  43. Limit search results to exact match
  44. What is an efficient way to query based on post_meta?
  45. Order posts by tags count?
  46. Merge 2 args in one WP_Query and order it by date
  47. WP_Query not working as expected for attachments and custom meta_query
  48. Orderby = none not working [duplicate]
  49. How to count post meta key values for all posts in database
  50. Highlighting Sub topic in a post?
  51. WP_query ‘orderby=none’ Problem
  52. Order posts by date and then by custom field
  53. How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
  54. Group posts by meta_key
  55. Pagination Not working on Home Page with 2 Query
  56. How should I use posts_where to change meta_value from a string to integer?
  57. Revolution Slider Orderby Two Custom Fields
  58. Multiple keyword search
  59. How can I create a WP_Query that returns posts where one meta_value
  60. Order posts ascending with number in title
  61. List taxonomy terms plus their latest post ordered by post date
  62. WordPress altering my custom query, How to fix it?
  63. WP Meta Query for some meta (array) values
  64. Get number of comments after modifying comment query
  65. Order by the results of a function with WP_query
  66. WP Query post meta value
  67. Would this post meta be better added to the post table rather than post_meta table
  68. Custom URl parameter
  69. Show default content if custom WP_Query has no posts
  70. query posts in functions.php and update a field
  71. Compile meta values from custom loop into array and then calculate sum total
  72. How to order posts in wp_query by a meta_value of the corresponding author
  73. How to get sum of meta_values of a meta_key in wp_query according to conditions
  74. how to fire join query with post_meta
  75. Order by menu structure
  76. Best approach to create Hot and Trending sections
  77. WP_Query, custom sort and custom filter
  78. WP_Query order by date in meta_value
  79. How to use order RAND() on WordPress?
  80. Order posts by meta value and Date
  81. Show all parents and children in custom post type in right order
  82. Efficient way to update multiple post meta
  83. Ordering by meta_value AND date NOT WORKING with wp_query
  84. WP_Query orderby breaks when using AJAX?
  85. Include Sticky Posts with Custom Query
  86. Order WP Query posts by custom order calculated from post meta values
  87. $wpdb->insert inserting two rows in database
  88. How to make orderby ‘meta_value_num’ OPTIONAL?
  89. Custom Post order for homepage
  90. Meta query with order by another custom field
  91. Change default ordering of taxonomy terms – pre_get_terms
  92. Query Posts depends on custom field inside repeater field using acf
  93. WordPress WP_Query orderby being overwritten
  94. WP_Query Order by Specific Post ID First
  95. Order Posts by meta value AND published date
  96. How can I pick a single post from the latest 3?
  97. Wp query orderby ‘title’ doesn’t work
  98. Query post with meta_query where date is not in future
  99. Minimising Database Queries when using Advanced Custom Fields
  100. How can I order Wp_Query hierarchically?
Categories wp-query Tags customization, order, post-meta, wp-query
How does WP determine the difference between a post and a page url
Remove a Plugin function through theme’s function.php?

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