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

WP_Query sort by meta_value_num or date

Got it working with the following method. There’s probably a better way to do it but this is good enough for it’s purpose.

Added IF statement for options

if($_POST['date'] === 'HIGH'){
    $order="meta_value_num";
    $sort="DESC";
}
elseif ($_POST['date'] === 'LOW'){
    $order="meta_value_num";
    $sort="ASC";
}
elseif ($_POST['date'] === 'ASC'){
    $order="date";
    $sort="ASC";
}
elseif ($_POST['date'] === 'DESC'){
    $order="date";
    $sort="DESC";
}

Then inside the $args

$args = array(
    'order' => $sort,
    'post_type' => 'product',
    'meta_key' => 'ek-product-price',
    'orderby' => $order,
)

Form

<select id="select1" type="text" name="date">
  <option value="ASC" type="hidden">ASC</option>
  <option value="DESC" type="hidden">DESC</option>
  <option value="HIGH" type="hidden">HIGH</option>
  <option value="LOW" type="hidden">LOW</option>
</select>

Related Posts:

  1. How can I sort posts by the date and a custom meta field?
  2. WP_Query orderby not working with meta_value_num and menu_order
  3. Creating Ordered Query using Meta_key
  4. How do I reorder (pop and push) items from WP_Query?
  5. Update all posts automatically when using post_meta
  6. Reverse chronology of post listing
  7. Better post meta efficiency?
  8. Change post format using custom field
  9. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  10. What Is meta_id In wp_postmeta?
  11. Add custom field automatically (add_post_meta) with value based on number of words of article
  12. Automatic value for custom fields for posts
  13. is there a way to show the the post title after the image?
  14. How do I correctly set up a WP-Query to only show upcoming event-posts?
  15. Query Posts With Over 1000 Views
  16. Cannot retrieve a custom RSS field from posts
  17. How to order posts by meta_value and title
  18. Query Posts by Custom Field
  19. how to get post order by post id wp_query?
  20. Customize rel=canonical tag for single blog post
  21. How to I retrieve the ID from the Posts page?
  22. Create a list of posts with topic headdings
  23. if in category but only with post meta
  24. wp_query with ajax
  25. Saving Post Data in Another Database
  26. Need to know custom code to display random and most viewed posts in wordpress posts and pages
  27. Add multiple meta keys to a post at once
  28. WP_Query custom field pass the post id
  29. saving/reading custom field value does not work – no value gets POSTed
  30. Override WordPress core post-template.php block template
  31. Post meta not working
  32. query posts with selected post ids first
  33. How do you update post date (year only) in a separate custom field?
  34. How to get value of a selected option from select tag and use it in WP_query to filter posts?
  35. How do I display posts ordered by a date custom field?
  36. Random order of the 5 most popular posts
  37. My custom query is displaying random posts in random order
  38. Get query result according to merged array
  39. Show posts between two Custom Fields date
  40. WP Query between posts custom fields [duplicate]
  41. Order posts by separate menu order for different sub-categories
  42. Order by ‘s’ using WP_Query()
  43. How enter custom post meta or custom field on box post list archive page?
  44. WP_query sort by custom meta_key “price”
  45. Image inside the content is replaced with featured image from my older post
  46. Order by Post Title in WP Query not working
  47. WP_Query – Order results by meta value
  48. How to limit the number of posts that WP_Query gets?
  49. How to update custom fields using the wp_insert_post() function?
  50. Can wp_query return posts meta in a single request?
  51. Should we trust the post globals?
  52. How to check if post meta key exists or not in wordpress database
  53. How to Change Order of Posts in Admin?
  54. Get current menu_order
  55. How to add category to: ‘wp-admin/post-new.php’?
  56. The next_posts_link() works only with original $wp_query
  57. How to protect pages with double authentication: password + email (in custom field)
  58. Order by optional meta key?
  59. Adding additional data to WP_Post object
  60. Get current post id in functions.php
  61. Random sort within an already sorted query
  62. How set featured posts using checkbox in post edit screen?
  63. Different post sort order within different categories
  64. Get first post from wp_query
  65. How can I retrieve multiple get_post_meta values efficiently?
  66. Code to make a post sticky
  67. WP_Query: Why is sticky post not first item in loop?
  68. WP_Query offset argument does not work
  69. Displaying the category name of a custom post type
  70. How to allow hidden custom fields to be added from wp-admin/post.php?
  71. what types of order can I pass to get_posts() via orderby?
  72. Query *only* sticky posts
  73. Add a checkbox to post screen that adds a class to the title
  74. Search by Hyphen
  75. Skipping first 3 posts in wp query
  76. Sort posts by popularity/page views
  77. Custom Field in Featured image for A particular post
  78. Export WordPress Posts and Meta Information in CSV format
  79. Sorting posts by custom date fields (non standard date format)
  80. Exclude drafts in all() view of edit.php
  81. Getting attachments by meta value
  82. Exclude posts that only have the ‘Uncategorized’ category [duplicate]
  83. Delete duplicated wp_postmeta record
  84. Accessing the post content with WP_Query
  85. Querying post from a multisite network
  86. How to exclude latest x posts from a paginated query?
  87. How to query serialized array by comparing greater than (>=)?
  88. Convert custom fields to post categories
  89. How to put last edited entry on top of the blog-post list?
  90. How to know if get_posts() failed?
  91. Show related posts by category but ignore one category
  92. meta_query where value is equal to given value
  93. Sort post by attributes ‘order’
  94. Will a large postmeta table slow a site down?
  95. post__in for get_posts with a dynamic array
  96. How to sort posts by last name (2nd word) on ONE category only?
  97. Query posts from current year
  98. How to batch update post content with custom post meta value
  99. Keep getting same permalink with WP_Query?
  100. Query posts distinct authors
Categories posts Tags custom-field, order, post-meta, posts, wp-query
How to Prevent User From Submitting Property Or View His Posted Properties Before Login in wordpress?
woocommerce post_type conflict with existing post type

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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