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

How to make orderby ‘meta_value_num’ OPTIONAL?

Short answer: You can’t. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort
http://us1.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that’s the price you pay for inconsistent data.

Related Posts:

  1. Sort by price wpdb
  2. wordpress sorting using array merge by price in ascending order but price with 0 must be show last
  3. How to only display posts whose meta_value field is not empty?
  4. Can wp_query return posts meta in a single request?
  5. order by numeric value for meta value
  6. Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
  7. How do I query for posts by partial meta key?
  8. Use REGEXP in WP_Query meta_query key
  9. WordPress retrieving meta data for all custom post types in list view
  10. Order by optional meta key?
  11. Sorting: custom query with orderby meta_value_num THEN by title
  12. meta_query: using BETWEEN with floats and/or casting to DECIMAL
  13. Reduce or prevent calling of update_meta_cache
  14. Is there a way to extend WP_query so Custom Post Types can have properties?
  15. Sort posts by category name and title
  16. How to get order of posts?
  17. What is the most efficient way of querying posts based on visits and date for current day?
  18. How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
  19. WP Query orderby meta key natural sort?
  20. Getting attachments by meta value
  21. WP_Query displaying ALL posts
  22. meta_query where value is equal to given value
  23. Query Custom Meta Value with Increment
  24. WP-CLI How to generate a list of posts with corresponding meta values
  25. Loop through all tags & output posts in alphabetical list
  26. What is an efficient way to query based on post_meta?
  27. WP_Query not working as expected for attachments and custom meta_query
  28. How to count post meta key values for all posts in database
  29. Sorting Posts by custom field
  30. Group posts by meta_key
  31. How should I use posts_where to change meta_value from a string to integer?
  32. How can I create a WP_Query that returns posts where one meta_value
  33. Sorting meta_value as integer doesn’t work
  34. WordPress altering my custom query, How to fix it?
  35. How to sort store location by specific category order in WP store locations
  36. how to include orderby value that is empty?
  37. How to sort wordpress posts already selected by WP_QUERY
  38. Meta query ignores multiple values of the key
  39. Custom Woocommerce Product Query
  40. Trouble with serialized metadata
  41. How sort products by calculate value? ( custom post meta, price, option)
  42. show most viewed post
  43. Display posts from multiple value in meta separated by comma
  44. Get authors list and sort them by recent posts
  45. Custom filter from post meta
  46. Move some posts to end of sort order, even if there is a sort in the wp_Query already
  47. How to sort by multiple values in a nested WP_Query
  48. Why can my filter query SOME metadata but not other metadata?
  49. Order by value of Custom Field using url string
  50. Query against multiple locations within single custom post type post
  51. WP Query with meta queries
  52. update_post_meta performance in a loop woocommerce
  53. update_post_meta performance in a loop woocommerce
  54. How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
  55. Order 2 meta_queries differently in WP_Query?
  56. I need query_posts() to order results first by a meta value and then by post ID
  57. Case insensitive ORDERBY in wpquery
  58. Sort posts on custom field AND after that sort on date?
  59. How to mix two orderby-parameters into one ordered result
  60. Look for string in posts and postmeta
  61. Query postmeta based on meta_value, return array of post_id
  62. update_post_meta() not updating
  63. Sorting Posts by Taxonomy thats not within the query’s $args
  64. I can’t get post based on its postmeta value and key
  65. Ignoring ‘a’ when sorting posts
  66. Saving custom fields for WP_Query to retrieve
  67. Meta key in wp_query bug?
  68. How to show all the associated posts with specific date of data metabox?
  69. Display three sequential posts on each page load, without repeating previous
  70. Combine WP_Query with array of custom data to single loop without breaking the pagination
  71. Wrap group of wp query posts to parent div by date/year
  72. WP Meta Query at depth 2
  73. WP_Query with child element
  74. Sort by meta key within same day
  75. Sorting posts by meta values: 2 different orders
  76. WP_Query Posts by Metadata from Option Tree
  77. Sorting Posts with meta value not working
  78. How to get several fields from wp_query?
  79. Sorting is not working in WordPress WP_Query
  80. How to sort a WP_Query array by post_name after an array_merge();
  81. WP Query to order posts by multiple meta fields
  82. Return a single custom post from multiple meta queries
  83. Custom Search Query – include only custom fields and title
  84. Sorting: custom query with orderby meta_value_num THEN by title
  85. query_posts with sorting on a custom datestamp
  86. How to count post meta key values for all posts in database
  87. How to query post ids liked by the Author
  88. Sort Posts with custom meta key by default which is currently set as optional
  89. loop through custom post types with meta data
  90. query by meta value then date and not empty meta value
  91. Query posts by meta value and sort by another meta key
  92. have_posts order by title descending
  93. WP_Query – order with usort by custom meta
  94. WP_Query by meta key not returning any posts
  95. Extend search query to search meta keys values based on search string
  96. Sort by multiple columns using get_posts
  97. How to get posts that have certain meta key value and order based on another meta key’s value
  98. WP Query order posts not working
  99. How to `’orderby’ => ‘meta_value_num’` in a series of orderby parameters
  100. query loop “inherit query from template” prevents setting sort order
Categories wp-query Tags post-meta, sort, wp-query
How do I grab specific posts (by post id) and display the title, featured image, and excerpt?
How to update records using $wpdb?

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