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

Ordering posts in Search & taxonomy by post_meta

You have a couple of issues with the code you are trying.

To check the query is for your CPT archive: You are using $query->is_post_type() but it doesn’t work (as far as I know, its not even a function). Instead you can use either:

if (is_post_type_archive('business')) {...}

or (if its not only on the archive page)

if ($query->get('post_type') == 'business') {...}

To order by meta value in pre_get_posts, you need to set the meta_key as well as the orderby (and optionally order).

Complete Function: Applying these to your “custom_special_sort” function, we get:

function custom_special_sort( $query ) {

    // if is this the main query and is this post type of business
    if ( $query->is_main_query() && is_post_type_archive( 'business' ) ) {

        // order results by the meta_key 'featured_listing'
        $query->set( 'meta_key', 'featured_listing' );
        $query->set( 'orderby', 'featured_listing' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'custom_special_sort' );

Related Posts:

  1. Ordering posts having multiple post-meta date fields
  2. Compare 3 custom fields and sort by oldest
  3. WP_Query of custom post type sorted by meta_key has unexpected results
  4. Query all posts where a meta key does not exist
  5. Adding ‘menu order’ column to custom post type admin screen
  6. Can the Next/Prev Post links be ordered by menu order or by a meta key?
  7. what is the correct way to compare dates in a WP query_posts meta_query
  8. Advanced search form with filters for custom taxonomies and custom fields
  9. Custom post type – order field
  10. Meta query with boolean true/false value
  11. Get post with multiple meta keys and value
  12. Correctly delete posts with meta and attachments [duplicate]
  13. Displaying a custom post type alphabetically
  14. Storing revisions of metadata for custom post type
  15. Order by & include array by specific post ids
  16. Custom field values deleted when trashing custom post type
  17. How to get all custom fields of any post type
  18. Update CPT meta data using REST API
  19. Admin Area Custom Type Search By Meta Fields Without Title & Content
  20. Echo all meta keys of a custom-post TYPE
  21. Custom Post type – how to get checkbox to update meta field to null
  22. How can I filter posts by post_parent in the admin?
  23. How do I Paginate Search Results for Custom Post Types?
  24. Sort Order for a Custom Query in a Post Type Archive Not Working
  25. Select All in Parent Category, Group by Child Category?
  26. Custom Post Type with Input fields to seperate table on database.
  27. How do I remove all the metaboxes for a custom post type?
  28. Custom Queries: Joining On Meta Values From Two Custom Post Types
  29. Gutenberg how to make attribute to save to meta
  30. How to programmatically create a connection with [Plugin: Posts 2 Posts] on cpt publish?
  31. Post metadata deletes itself
  32. How to create a mini directory in WordPress?
  33. How Can I save multiple records in same meta key?
  34. Order Posts by Taxonomy and Meta Value
  35. WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
  36. Get all posts from custom post type and insert in select input as metabox
  37. Detect meta value changes when post is updated (post_updated)
  38. Custom Post type sort order not working in the admin area
  39. Search everything (posts, pages, tags, cpt, meta)
  40. Permalinks using event date (year & month) instead of publication date
  41. Order posts by (hierarchical custom) taxonomy terms and term children
  42. Custom Post Type: Set post_title equal to a custom post type field
  43. I can’t set meta_key in my custom post type query
  44. Grossly inefficient wordpress loops!
  45. List upcoming events, ordered by date in a custom field
  46. How Do I Use WP_Query to Run This Database Query as Search Result?
  47. WordPress Admin Panel search posts with custom post meta values along with title
  48. Creating a custom post type upon registration
  49. How to Update post status using meta data in Custom post TYpe
  50. Rest Api v2 orderby meta_key in custom post type
  51. bulk Update post_meta with default values
  52. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  53. Can’t sort order of wp_query with 2 meta keys
  54. Showing Meta Data for Custom Post Types?
  55. ascending order custom post type
  56. How to get source of custom meta image?
  57. WP_Query post at custom position
  58. Sort custom post types by last name in the backend
  59. Having trouble with custom post type / meta box
  60. Query for posts from any post type but only add instock products
  61. How can I include meta box content when searching?
  62. query order by date on custom type: wrong order
  63. update a post meta from a single table cell TablePress
  64. update custom post type meta from a shortcode
  65. Taxonomy list. Order by a specific custom post type count
  66. Custom Meta Boxes – Nonce Issue – Move to trash issue
  67. Displaying Meta Box Image
  68. WooCommerce – Show orders for a specific product ? [closed]
  69. Custom post type category not displaying in custom post type
  70. How to make custom post meta wp_editor translatable?
  71. Re-order posts in query after
  72. Automatically adding meta data to posts or multiple query help
  73. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  74. How to add multiple featured image in meta box in post editor?
  75. Show metabox in custom-post-template depending on taxonomy term?
  76. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  77. update_post_meta() whenever custom post type is updated
  78. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  79. Trigger “unsaved changes” dialog for custom post meta changes
  80. Using date stored as custom field to filter posts displayed in admin
  81. Conditionally Query Custom Post Types by Post Meta for Blog Home Page?
  82. Display custom post types with custom date field value (before today) & order by custom date field
  83. Displaying custom posts only if custom meta box’s date is not expired
  84. Displaying Metabox value (custom post type taxonomy)
  85. Custom Post Type order by modified date and custom meta field issue
  86. How do I sort a custom post type admin column using two meta keys?
  87. Add Post Url to Manage Edit Screen
  88. How to best delete orphan wp_postmeta
  89. How to query different post types in specific order?
  90. How can I display my custom metaboxes on a custom post template?
  91. Display all custom post type posts and order them by an optional meta_key
  92. Meta Box Only in CPT Edit Page, Not in Add New
  93. get_post_meta is returning image id
  94. Custom Post Type, Saving Multiple Checkboxes
  95. get_template_part() – post-meta not working?
  96. How can I get_post_meta() in the most efficient way?
  97. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  98. Count posts with specific term_meta
  99. Custom search for custom post meta with pre_get_posts interferes with WP search
  100. Create a random unique 6 digit number as custom field for custom post type
Categories custom-post-types Tags custom-post-types, order, post-meta
WordPress Admin Not Loading All Core Scripts
Post thumbnail not showing in WP admin area for custom 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