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 custom meta_key “price”

Thanks to Brad Dalton at wpsites.net for getting me to the answer.

  1. Install Advanced Custom Fields plugin. Create a custom field called price, formatted as a number.

  2. Use the code below (based on Bill Erickson’s tutorial):

add_action( 'pre_get_posts', 'kr_change_posts_order' ); 
function kr_change_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() && !is_page() ) {
        $orderby = genesis_get_custom_field( 'price' );
        $query->set( 'cat', ( array( 5, 6, 15 ) ) );
        $query->set( 'meta_key', 'price' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
    }
}

This sorts by price, descending, for three post categories with IDs 5, 6 and 15.

Related Posts:

  1. How to exclude latest x posts from a paginated query?
  2. WordPress Number of Posts Not Changing With posts_per_page
  3. Replace Main Query With Custom Query
  4. Custom Order in WP Query
  5. how to get posts ids inside pre_get_posts filter?
  6. How can I sort posts by the date and a custom meta field?
  7. is there a way to show the the post title after the image?
  8. How to exclude latest x posts from a paginated query?
  9. wp_query with ajax
  10. How do I display main query posts in random order using add_filter
  11. WP_Query sort by meta_value_num or date
  12. Creating Ordered Query using Meta_key
  13. Show posts between two Custom Fields date
  14. How to get Tags with specific post id
  15. how can i change WP main archives loop to sort by name or title
  16. How to automate featured posts number? [duplicate]
  17. Using wp_list_pages() after calling query_posts()
  18. how to get post order by post id wp_query?
  19. Possible to alternate between two loops
  20. Nested WP_Query breaking loop
  21. WordPress query_posts by tag doesn’t work anymore(?)
  22. How to display all posts divided/ordered by post date? [closed]
  23. Why Query is returning empty array?
  24. How do I extract just the post ID of the first item in whatever WP_Query returns?
  25. Show post content and title in diferent divs using WP_Query using a loop
  26. Customize rel=canonical tag for single blog post
  27. Change all author links in Blog roll
  28. How to I retrieve the ID from the Posts page?
  29. How to save meta checkbox WordPress
  30. Why are my paginated posts always returning the same results?
  31. Ignore latest two posts
  32. Is it possible to use WP_Query to only pull posts with attachments?
  33. Show posts by author of membership level (Paid Membership Pro)
  34. Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
  35. meta_value timestamp older than now
  36. How to calculate the average of a post meta value(Numeric) of a specific author
  37. Every second post different class in blog view
  38. Loop doesn’t exclude the specified category in home page
  39. Get posts with multiple categories
  40. Do not show children of a category
  41. Is there any way to tell when wp_postmeta has been updated?
  42. post__in not working with pre_get_posts, but post__not_in does work
  43. Display posts of specific category term
  44. Print out last 3 blogposts
  45. why the same code got different results when using query_posts in functions.php and index.php
  46. Query posts by Author and/or by Tag
  47. Query for first 3 posts to change the look and feel
  48. How to get posts published on the latest date?
  49. wpColorPicker – problem with implementation to post meta
  50. Why posts array is empty?
  51. Can’t understand why sometimes a [caption] field appears
  52. Page and post loop same template
  53. Getting blog pagination to work on page set as front page
  54. How to group only VISIBLE posts? [closed]
  55. How to check in functions.php if there is data in a WP_Query?
  56. Shortcode with ‘year’ parameter
  57. search blog posts by author first name and or last name
  58. Change post author without using wp_update_post()
  59. Show metabox value last post excerpt, title and link
  60. if in category but only with post meta
  61. On what hook can I get the queried object’s final state?
  62. unique post in “$args” of wp_query display more than one post
  63. Display current post position in Elementor Posts widget
  64. Why in my query is display two title?
  65. Migrate posts from category and sub-category via SQL
  66. How do I insert a after every 5 posts in a WordPress Loop that infinitely loads posts?
  67. How to give classname to post if post has no content?
  68. Update post meta within save_post action
  69. Custom wp-query in wordpress rest api
  70. Display post number by category
  71. Updating post meta for checkbox
  72. Saving Post Data in Another Database
  73. WP_Query: Show 10 posts in date order, first three random
  74. How can I insert a new markup element after 4 posts automatically? [closed]
  75. Exclude some posts from displaying in wp_query based on some condition
  76. posts_per_page returning only one post
  77. Modify main query before it run without pre_get_post
  78. If no posts exist for current day and month, show the next available day of posts
  79. WordPress Loop: How to display recent posts in multiple divs
  80. Random posts that always include a particular post?
  81. Need to know custom code to display random and most viewed posts in wordpress posts and pages
  82. Related Posts function not working
  83. How to retrieve certain number of images from a wordpress post?
  84. WordPress post filter menu
  85. Custom Post Limit for homepage only without plugin?
  86. Custom permalink for each post
  87. How can I tell if a post has been published at least once?
  88. Is there a way getting post fields using `transition_post_status`
  89. Automatic blog page with custom design [closed]
  90. How to display post list in a table layout (multiple queries in single loop)
  91. Conflict array_splice on loop and query in widget
  92. Add multiple meta keys to a post at once
  93. Show number of posts by logged in user
  94. How does WP decide how to display a page
  95. Not all posts showing in query
  96. how to handle the loop using filling bootstrap grid structure?
  97. Get all custom posts with a certain taxonomy
  98. WP_Query custom field pass the post id
  99. How to convert query sql to shortcode in wordpress?
  100. Link to date_query results in WordPress
Categories posts Tags post-meta, posts, pre-get-posts, wp-query
is it possible to create a website with wordpress with these requirements without coading?
change template with button

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