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

Order by post meta value gets random results

Why are you using SQL when there is our Codex!

Use this code snippet instead-

function pre_change_query( $wp_query ) {

        $wp_query->set('meta_key', 'package_id');
        $wp_query->set('orderby', 'meta_value');
        $wp_query->set('order', 'DESC'); // or 'ASC'

}

add_filter('pre_get_posts', 'pre_change_query');

$args = array(
    'post_type' => 'estate_agent',
    'posts_per_page'    => 10
);

$my_custom_query = new WP_Query( $args );

See Codex for details: https://codex.wordpress.org/Class_Reference/WP_Query

Related Posts:

  1. Can the Next/Prev Post links be ordered by menu order or by a meta key?
  2. Correctly delete posts with meta and attachments [duplicate]
  3. Storing revisions of metadata for custom post type
  4. Ordering posts having multiple post-meta date fields
  5. Custom Post Type with Input fields to seperate table on database.
  6. How Can I save multiple records in same meta key?
  7. Permalinks using event date (year & month) instead of publication date
  8. Grossly inefficient wordpress loops!
  9. Creating a custom post type upon registration
  10. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  11. Showing Meta Data for Custom Post Types?
  12. Having trouble with custom post type / meta box
  13. Query for posts from any post type but only add instock products
  14. update custom post type meta from a shortcode
  15. How to make custom post meta wp_editor translatable?
  16. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  17. How to add multiple featured image in meta box in post editor?
  18. Show metabox in custom-post-template depending on taxonomy term?
  19. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  20. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  21. Using date stored as custom field to filter posts displayed in admin
  22. How to best delete orphan wp_postmeta
  23. Meta Box Only in CPT Edit Page, Not in Add New
  24. get_template_part() – post-meta not working?
  25. WordPress Orderby Numeric Value Not Working
  26. Replace title column in post list with post meta value
  27. Does WordPress limit the length of slug names for Post Meta or Terms?
  28. Populate Custom Fields in a Custom Post Type?
  29. Insert Multiple Post with Same Publish Button
  30. Custom meta box data array: foreach not working correctly?
  31. Undefined Variable – Custom Post Type Meta
  32. Ordering posts in Search & taxonomy by post_meta
  33. How to check if user meta field is empty in conditional else statement
  34. Post image in WordPress not appearing on home page
  35. Getting meta in editor plugin, and event triggering issue
  36. Restrict Access to Posts based on Custom User and Post Meta Data
  37. Should wp_postmeta meta_key always be unique for a given post_id?
  38. Metabox not show in categories custom post type cmb2
  39. Getting thumbnails of a custom posts featured images in a page-templates metabox
  40. Remove Post Custom Meta Box
  41. Using preg_replace to filter custom textarea
  42. Check if post with same meta value exists
  43. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  44. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  45. Archieve.php not loading for custom post type
  46. Meta data (Tags and Categories) for Custom Posts not showing.
  47. Display different information of a custom post type
  48. Create Array from custom post type to display a slider
  49. delete_post_meta() for whole CPT / multiple posts?
  50. How to get specific post meta by title or id
  51. How can I display a drop-down select of Post Names
  52. how to show records that don’t have custom meta value
  53. $post->post_meta not pulling any post in wordpress/php
  54. WP_Query of custom post type sorted by meta_key has unexpected results
  55. Cannot obtain custom meta information
  56. Get all metadata for a given custom post type
  57. Advanced custom field – posted fields from custom post type
  58. delete duplicate meta_value with same post_id
  59. Search CPT Title AND Meta
  60. Add a class to post_class if more than one post shares same meta_value_num
  61. Function to return values from metabox
  62. Add a meta field to the list of results for a custom post type
  63. Empty meta-box returns publishdate if no value is set?
  64. Filter search posts by post meta?
  65. Custom fields for post or terms which don’t update on post update
  66. Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
  67. Add a form in every post and save data in post meta
  68. How to use multiple Meta Field from CPT as Post permalink
  69. Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
  70. Attaching more than 1 parent (Post) to a media file
  71. Use WP_query to match post types based on custom field values
  72. How can I add a meta[] to my custom post type and search by term with the Rest API?
  73. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  74. Custom meta fields not showing up in WP_Response Object via custom endpoint
  75. How to get post by meta value
  76. Get meta values from parent post and save in child post
  77. How do I get all authors posts of a custom post type outside loop
  78. How to Disable option of meta field if that option is selected for any other post in custom post type?
  79. How to get post detail based on URL of the post
  80. Stored meta from attachment, video length?
  81. Custom post type meta fields missing on save_post_type
  82. Linking posts together with Advanced Custom Fields “both ways”
  83. Order posts by meta value hiding posts instead of re-ordering
  84. custom search results – order results differently by post type
  85. How to get Current week and current date record wp query
  86. get_post_meta for Custom Post Type ( CPT )
  87. Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
  88. How to create content automatically when a post is published?
  89. Rich Custom Field for Custom Post type not saving
  90. Add auto increment value to custom meta
  91. Return only custom post types for the page, not all
  92. Retrieve post data via WPDB class
  93. Create action running on trashed_post hook to modify post_meta value
  94. Random meta field from specific custom post type
  95. Custom Post Type meta value is being saved in array, instead of just the string (as value)
  96. Custom Form Data save after Login User
  97. Changing to different post type maintains the redirection?
  98. How do I add the “orange counter” next to a Custom Post Type?
  99. Quering a post object based on another related post object
  100. rest_api_init is not getting invoked inside a Class
Categories custom-post-types Tags custom-post-types, post-meta
Loop doesn’t work
Relationship between custom post type and page/single templates

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