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

Sort by price wpdb

Replace

 $args  = array( 
                 'post_type'           => array('product', 'product_variation'),
                'post_status'         => 'publish',
                'posts_per_page' => get_option('posts_per_page'),
                'paged'          => $paged,
                'tax_query'      => $tax_query,
                'orderby' => 'price',
                 'order' => 'asc',
                            
                  );

New code


  $orderby = $_GET['orderby'] ?? '';
   
  $args  = array( 
                'post_type'           => array('product', 'product_variation'),
                'post_status'         => 'publish',
                'posts_per_page' => get_option('posts_per_page'),
                'paged'          => $paged,
                'tax_query'      => $tax_query
             );
                  
  if ($orderby == 'price') {
    
        $args['orderby'] = 'meta_value_num';
        
        $args['meta_key'] = '_price';
        
        $args['order'] = 'asc';
  
  } elseif ($orderby == 'price-desc'){
        
        $args['orderby'] = 'meta_value_num';
        
        $args['meta_key'] = '_price';
        
        $args['order'] = 'desc';
        
  } elseif ($orderby == 'menu_order') {
    
    // sort menu order
    
  } elseif ($orderby == 'popularity') {
    
    // sort popularity
    
  } elseif ($orderby == 'rating') {
    
    // sort rating
    
  } elseif ($orderby == 'date') {
    
    // sort date
  }

Related Posts:

  1. How to make orderby ‘meta_value_num’ OPTIONAL?
  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. WP Meta Query for some meta (array) values
  36. WP Query post meta value
  37. Sort in WP_Query(), not filter? Is it possible?
  38. Would this post meta be better added to the post table rather than post_meta table
  39. Custom URl parameter
  40. query posts in functions.php and update a field
  41. Compile meta values from custom loop into array and then calculate sum total
  42. How to get sum of meta_values of a meta_key in wp_query according to conditions
  43. how to fire join query with post_meta
  44. Front End Sorting Questions
  45. Woocommerce custom loop to show all the products [closed]
  46. Sort by meta key on archive page
  47. Best approach to create Hot and Trending sections
  48. WP_Query, custom sort and custom filter
  49. How to sort posts in admin by titles with dd.mm.yyyy format?
  50. Query Set Order By Author
  51. WordPress: Keep order of query_posts list of post ID’s using post__in
  52. Order posts by meta value and Date
  53. Efficient way to update multiple post meta
  54. Meta query with order by another custom field
  55. Query Posts depends on custom field inside repeater field using acf
  56. Query post with meta_query where date is not in future
  57. Minimising Database Queries when using Advanced Custom Fields
  58. Get meta info related to current post
  59. How to orderby meta_value_num with dollar ($) sign
  60. Select from wp_post and multiple meta_value from wp_postmeta
  61. How do I sort posts with multiple pages
  62. Pull post meta with post_query?
  63. Group WP_Query by meta_key date
  64. WP_query posts closest to todays date
  65. WP_Query posts by distance based on LAT & LNG in Database
  66. exclude posts with a specific custom field and value
  67. Changing WP_Query params with url Query Var
  68. Inserting serialized value into wp_postmeta using update_post_meta
  69. How to make Meta Query case sensitive?
  70. Performance when getting post meta for post retrieved by meta value
  71. Get attachment by meta_key value
  72. Display custom field meta outside loop, site wide
  73. Using hook to use DISTINCT in a wp_query
  74. Fetch Record based on meta key dates
  75. Sort wordpress custom posts based on meta value
  76. WP_Query: Group events by year, sorted DESC; then by date for each year group, sorted ASC
  77. WordPress Comments sort by custom meta key
  78. Understanding the orderby in WP_Query?
  79. Where to put meta Keys
  80. how to make members list directory through wordpress post custom meta key.
  81. WP_query sorting can’t sort danish letters (æ, ø, å)
  82. How do I query for posts by partial meta key?
  83. Large AND OR query timing out
  84. Help ordering Post loop by two meta values
  85. get_post_meta slowing down my page load (in a plugin)
  86. New WP_Query loop in admin causes problems
  87. How to sort a custom wordpress query by combination of meta values?
  88. echo a specific meta_key queried through a custom post
  89. Gallery shortcode numerical sorting
  90. How to increase load time of an archive/search page (WP_Query)
  91. WP Group posts by year(desc) > month(desc) > date(asc)
  92. get_post_meta in WP_Query
  93. Which is faster wpdb & get_row or wp_query & ge_post_meta?
  94. Does wp track views for posts?
  95. Hide posts with meta key in WP_Query
  96. Sort WordPress Posts Meta value by Week not Day
  97. Filter posts by comparing custom meta value against postdate
  98. WordPress query posts with multiple post_meta data
  99. How to order WP_Query to group results?
  100. WP_Query – Accessing MetaValue from Query Result
Categories wp-query Tags page-specific-settings, post-meta, sort, wp-query
WordPress Query for CPT that only shows posts within radius of current user’s geolocation
Cannot pass value variable to WP AJAX functions

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