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. Get current post id in functions.php
  47. Code to make a post sticky
  48. Sorting posts by custom date fields (non standard date format)
  49. Sort post by attributes ‘order’
  50. Will a large postmeta table slow a site down?
  51. Keep getting same permalink with WP_Query?
  52. How to query posts with current or future date only
  53. Pagination not working on Custom Page Template
  54. Create new WordPress post [wp_insert_post] based on results of a WP_Query
  55. Display custom post types by date field
  56. How to change what the post creation page looks like?
  57. Exporting Data from WordPress into a flat table
  58. Custom Order in WP Query
  59. Search query – exact post title match
  60. How to get most recent commented post above new submitted post in WordPress?
  61. Display posts from #6 to #20 on archive page
  62. How to start with post number x?
  63. Automatic post deletion
  64. Change post_date to post_modified date on post template?
  65. Duplicating Table of Contents for Paginated Post
  66. How can I show many posts an author has per week?
  67. How to exclude latest x posts from a paginated query?
  68. Order by empty custom field
  69. Pausing and Resuming WP_Query results
  70. How to : pagination in 3 different custom loops on the same page
  71. Custom WP_Query for WordPress Search Results with meta_query
  72. Custom order for Mysql array
  73. How Do I Get My Post Tags to Alphabetize?
  74. Get posts with multiple categories
  75. Do not show children of a category
  76. Add field to user meta table in database when link is clicked
  77. Hide custom metabox value from custom fields
  78. How to give classname to post if post has no content?
  79. Custom wp-query in wordpress rest api
  80. How to retrieve certain number of images from a wordpress post?
  81. WordPress post filter menu
  82. How can I tell if a post has been published at least once?
  83. Retrieve data from post meta meta_key having ‘_product_fq_image’ while having author_id and post type is PRODUCT
  84. Current and next month posts
  85. Adding a meta box to determine the sidebar [closed]
  86. Same post appears in related Posts?
  87. Get post category as a separate string and url
  88. WP_Query outputs wrong post in custom post type
  89. How do i create a custom post query when the meta value is an array?
  90. What is proper filter or hook action to add data to the $post object?
  91. Order by meta_key field in WordPress not meta_value field value
  92. How to remove a post from results by ID after query?
  93. post_name is not stored until post is published… where should I store my slug until it goes live then?
  94. WordPress loop uses unmodified posts array, why?
  95. Sorting Posts with meta value not working
  96. 404 on Pages for Custom Post Type & Query_Posts
  97. get_post_meta property of non object
  98. First post in loop displays twice
  99. If meta_key exists then don’t select this post?
  100. How to make WP_Query faster when getting a post from one year ago?
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
  • 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