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

mySQL query. ORDER BY meta_key

Zlatev

meta_key is the name of the custom post type, and is a VARCHAR(255)
meta_value is the value of the key, and is a longtext

With your query you are not ordering by values, but by the name of the field. The query works for _thumbnail_id because you are using LEFT JOIN and the CPT could be null.

You could see if you run this query

SELECT SQL_CALC_FOUND_ROWS wp_posts.* ,m.*
FROM wp_posts  
LEFT JOIN wp_postmeta m ON ( wp_posts.ID = m.post_id AND m.meta_key = '_thumbnail_id')
ORDER BY 
    m.meta_key ASC,
    wp_posts.post_date DESC

See the null for the meta_key

In the second case, the CPT _vip_post IS always present, so the ordering on the fieldname is not working. You must order it by meta_value.

Try something like:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* ,m.*
FROM wp_posts  
LEFT JOIN wp_postmeta m ON ( wp_posts.ID = m.post_id AND m.meta_key = '_vip_post')
ORDER BY 
    m.meta_value ASC,
    wp_posts.post_date DESC

You can change the ordering by using *m.meta_value DESC*.

Related Posts:

  1. Filter query posts by the count of metas for a meta key
  2. Grouping related postmeta data via SQL query
  3. Custom MySQL Query for Post and Post Meta
  4. simple sql query on wp_postmeta very slow
  5. wordpress query – orderby child post date
  6. How many WordPress SQL Queries per page?
  7. Search custom post type by meta data
  8. Multipart/formatted MySQL query problem
  9. Query by one meta_key and sort by another (possibly NULL value)
  10. How to display liked posts of current user in wordpress?
  11. Are database queries created using WordPress filters protected from SQL injection?
  12. can’t get query to order posts by acf datepicker
  13. Save default value for empty or missing post meta
  14. Mysql / WordPress killing my server with 80k users [closed]
  15. Slow meta_query with about 4 milion record on wp_postmeta
  16. Select User by Joining Multiple Meta Value Results
  17. Remove posts from query for events whose start date has passed
  18. Update slug (URL) of pending posts via phpMyAdmin
  19. My SQL function to change user_name
  20. Query metas (and not : query posts by metas)
  21. You have an error in your SQL syntax – Help with query
  22. Order query by post meta value
  23. MySQL variable in query
  24. how would I create a custom query to get all users, and a related post based on a postmeta field?
  25. Help with MySQL to $WPDB query
  26. SQL query to select posts from multiple categories
  27. Order posts by custom field DATE value
  28. Ordering by a metadata subfield in WordPress?
  29. How do I see the mysql query generated by get_posts( $args )?
  30. Alter query with posts_clauses to retrieve NULL values last
  31. Order by summing multiple values
  32. Issue with using add_rewrite_rule() for post querying custom fields
  33. MySql Query very slow
  34. Searching With Apostrophe
  35. WordPress Query wp-terms SLOW
  36. Query posts according to specific post meta values
  37. wpdb COALESCE won’t work
  38. Order by meta value pro first then meta value free in my search function
  39. Order by title – but now built in wordpress func, sort the_title
  40. Get posts from category with custom query
  41. how to insert missing tags into the posts through mySQL?
  42. Ideas how to search & replace post_content when string contains a newline?
  43. Valid SQL query return empty
  44. mysql query – how to escape apostrophe?
  45. Query the WordPress database to get data together with replaced information
  46. Optimize slow SQL query for multiple meta values
  47. Updating with $qpdb->query() always returns 0 rows affected
  48. Querying with WP query using meta key price
  49. How to manage a particular “order by” for get_search_query()?
  50. get_post_meta with WP_query
  51. Order terms by count – missing terms
  52. Declaring a var, placing it in a query and using the output of the query?
  53. How to return count of custom post type posts with a specific custom field value via $wpdb?
  54. WordPress Mysql query and Duplicate
  55. Help with Related Posts Function
  56. Combining two meta_values within one row with query
  57. JetEngine Query Builder – order by value from repeater
  58. How to delete all images from code in all post_content
  59. Sorting my posts on homepage my specific value in post_meta table
  60. I want to get on those users their meta value are like “AGENT” .. but this query is not working
  61. Order posts ascending from posts in array
  62. Displaying data from custom table
  63. Get 2 meta values from meta key column
  64. wp query multiple values > display a specific value first
  65. PHP Fatal error: Uncaught Error: Call to a member function insert() on null
  66. SQL query to delete users with multiple meta keys and comments
  67. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  68. Sort by postmeta on when searching
  69. OR condition not working
  70. Trouble migrating custom post types from non-wordpress cms
  71. Order WordPress Query by Custom Field While Still Using Tax_query Argument
  72. “order” does not affect order of custom query
  73. Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
  74. Performance of wp_get_attachment_image_srcset() and wp_get_attachment_image_url()
  75. How to make MySQL search queries with quotes
  76. how to get postmeta info based on multiple categories?
  77. Use $wpdb->get_results with filter based on array
  78. Query Posts that have or don’t have a meta_value and order by the same ASC
  79. What is the best way to query posts based on live data?
  80. Apply ordering args to newly merged queried
  81. Mysql query and odd results
  82. sql for querying post and their category
  83. Access Tables with number prefix
  84. WP_Query orderby meta key/value suddely stopped working
  85. Ascending & descending posts on same page with IF statement
  86. is it possible to get a list of URLs from post_content directly in a mysql query via phpadmin?
  87. Custom API how to return int field
  88. Problem in MySql query on WordPress [closed]
  89. meta query always fails even if data is correct
  90. How can I display a number to represent the order of post?
  91. Sorting of coupons in the right way
  92. Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice
  93. How to query custom post type one after another
  94. How to get several fields from wp_query?
  95. How do i get value from wp_postmeta?
  96. Using cron for multiple queries
  97. Ordering posts alphabetically by meta keys and title
  98. How to display a specific category using a custom Query in WordPress?
  99. Update vs Insert logic but the last key is always inserted?
  100. How to order posts by the user_login that corresponds to a meta_value (which is a user id)?
Categories query Tags mysql, order, post-meta, query
a plugin to share a post to social networks via admin? [closed]
Multi-site vs seperate installs for just 3 blogs?

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