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:
- Can the Next/Prev Post links be ordered by menu order or by a meta key?
- Correctly delete posts with meta and attachments [duplicate]
- Storing revisions of metadata for custom post type
- Ordering posts having multiple post-meta date fields
- Custom Post Type with Input fields to seperate table on database.
- How Can I save multiple records in same meta key?
- Permalinks using event date (year & month) instead of publication date
- Grossly inefficient wordpress loops!
- Creating a custom post type upon registration
- Reason action hook won’t work with update_post_meta from frontend form? Alternative?
- Showing Meta Data for Custom Post Types?
- Having trouble with custom post type / meta box
- Query for posts from any post type but only add instock products
- update custom post type meta from a shortcode
- How to make custom post meta wp_editor translatable?
- How to keep a check box in custom meta box for custom post type checked by default for add new post?
- How to add multiple featured image in meta box in post editor?
- Show metabox in custom-post-template depending on taxonomy term?
- ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
- Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
- Using date stored as custom field to filter posts displayed in admin
- How to best delete orphan wp_postmeta
- Meta Box Only in CPT Edit Page, Not in Add New
- get_template_part() – post-meta not working?
- WordPress Orderby Numeric Value Not Working
- Replace title column in post list with post meta value
- Does WordPress limit the length of slug names for Post Meta or Terms?
- Populate Custom Fields in a Custom Post Type?
- Insert Multiple Post with Same Publish Button
- Custom meta box data array: foreach not working correctly?
- Undefined Variable – Custom Post Type Meta
- Ordering posts in Search & taxonomy by post_meta
- How to check if user meta field is empty in conditional else statement
- Post image in WordPress not appearing on home page
- Getting meta in editor plugin, and event triggering issue
- Restrict Access to Posts based on Custom User and Post Meta Data
- Should wp_postmeta meta_key always be unique for a given post_id?
- Metabox not show in categories custom post type cmb2
- Getting thumbnails of a custom posts featured images in a page-templates metabox
- Remove Post Custom Meta Box
- Using preg_replace to filter custom textarea
- Check if post with same meta value exists
- If Post Meta Equals Current Title Conditional Advanced Custom Fields
- Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
- Archieve.php not loading for custom post type
- Meta data (Tags and Categories) for Custom Posts not showing.
- Display different information of a custom post type
- Create Array from custom post type to display a slider
- delete_post_meta() for whole CPT / multiple posts?
- How to get specific post meta by title or id
- How can I display a drop-down select of Post Names
- how to show records that don’t have custom meta value
- $post->post_meta not pulling any post in wordpress/php
- WP_Query of custom post type sorted by meta_key has unexpected results
- Cannot obtain custom meta information
- Get all metadata for a given custom post type
- Advanced custom field – posted fields from custom post type
- delete duplicate meta_value with same post_id
- Search CPT Title AND Meta
- Add a class to post_class if more than one post shares same meta_value_num
- Function to return values from metabox
- Add a meta field to the list of results for a custom post type
- Empty meta-box returns publishdate if no value is set?
- Filter search posts by post meta?
- Custom fields for post or terms which don’t update on post update
- Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
- Add a form in every post and save data in post meta
- How to use multiple Meta Field from CPT as Post permalink
- Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
- Attaching more than 1 parent (Post) to a media file
- Use WP_query to match post types based on custom field values
- How can I add a meta[] to my custom post type and search by term with the Rest API?
- Creating an archive page or simple template to list all values of a custom field of specific post type listing
- Custom meta fields not showing up in WP_Response Object via custom endpoint
- How to get post by meta value
- Get meta values from parent post and save in child post
- How do I get all authors posts of a custom post type outside loop
- How to Disable option of meta field if that option is selected for any other post in custom post type?
- How to get post detail based on URL of the post
- Stored meta from attachment, video length?
- Custom post type meta fields missing on save_post_type
- Linking posts together with Advanced Custom Fields “both ways”
- Order posts by meta value hiding posts instead of re-ordering
- custom search results – order results differently by post type
- How to get Current week and current date record wp query
- get_post_meta for Custom Post Type ( CPT )
- Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
- How to create content automatically when a post is published?
- Rich Custom Field for Custom Post type not saving
- Add auto increment value to custom meta
- Return only custom post types for the page, not all
- Retrieve post data via WPDB class
- Create action running on trashed_post hook to modify post_meta value
- Random meta field from specific custom post type
- Custom Post Type meta value is being saved in array, instead of just the string (as value)
- Custom Form Data save after Login User
- Changing to different post type maintains the redirection?
- How do I add the “orange counter” next to a Custom Post Type?
- Quering a post object based on another related post object
- rest_api_init is not getting invoked inside a Class