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 posts by meta value hiding posts instead of re-ordering

Literally seconds after updating this post I found the solution….again, Must have looked at this previously and not entered the details correctly as I tried this one before. 🙂

    add_action('pre_get_posts', 'add_special_sort', 11, 1);
function add_special_sort($query){
  // Bail if not the main "hidden" query, as opposed to a 'new WP_Query()' call
  if(!$query->is_main_query())
    return;

  $query->set('meta_query', array(
    'relation' => 'OR',
    array(
      'key' => 'featured_listing',
      'compare' => 'NOT EXISTS',
    ),
    array(
      'key' => 'featured_listing',
      'compare' => 'EXISTS',
    )
  ));
  //$query->set('meta_key', NULL);
  $query->set('orderby', array('meta_value_num' => 'DESC', 'date' => 'DESC'));
}

Related Posts:

  1. Advanced search form with filters for custom taxonomies and custom fields
  2. Custom Meta Boxes – Nonce Issue – Move to trash issue
  3. Show metabox in custom-post-template depending on taxonomy term?
  4. Displaying Metabox value (custom post type taxonomy)
  5. How can I display my custom metaboxes on a custom post template?
  6. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  7. Count posts with specific term_meta
  8. Does WordPress limit the length of slug names for Post Meta or Terms?
  9. How to duplicate entire custom post type
  10. Metabox not show in categories custom post type cmb2
  11. Display different information of a custom post type
  12. Filter posts by tax (dropdown) and meta value
  13. how to show records that don’t have custom meta value
  14. best way to use custom taxonomy, post type and meta in a job system
  15. WordPress CPT Taxonomy Dashboard Search – How to include taxonomy in search?
  16. Custom Post Type meta data getting deleted on bulk editing taxonomies
  17. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  18. Saving Child Terms on front end not setting parent
  19. Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
  20. WordPress custom post type
  21. Values inside a custom field to determine which category posts to display
  22. Timetable of Custom Meta Data using Custom Post Type and Custom Taxonomy
  23. Advanced search form with filters for custom taxonomies and custom fields
  24. Automatically add custom taxonomy when meta value gets to a set number or beyond
  25. Custom post type and custom taxonomy with the same slug using the file page taxonomy only as directory
  26. What is the best practice for displaying my plugin content in themes?
  27. Categorize custom post type
  28. WordPress request fiter order by related post’s post_title
  29. How do I display the taxonomy for a custom post type in an array
  30. Pretty URL for custom search for custom post type
  31. Custom taxonomy rewrite with query var returns %2F in URL
  32. WP Rewrite Rule Issue while using Custom post type & taxonomy
  33. How to list custom taxonomies in the back end
  34. Several CPT’s with same Taxonomy name but with different Terms for each
  35. Remove slug in URL custom post type
  36. Custom Permalinks with CPT and Hierarchical Taxonomies
  37. How to conditionally redirect to the post from a taxonomy page?
  38. How do I check if the user is on a taxonomy term parent, child or grandchild page?
  39. Sort and filter custom post type posts by custom taxonomy
  40. How do I display the grand child items of a taxonomy term?
  41. How to edit this code to get the categories in achieve page?
  42. Taxonomies relations
  43. Custom post type editor with dynamic selects, one drop down populating a second second drop down not working
  44. Is it better to use WordPress Custom Post Types or Taxonomies?
  45. Get general taxonomy name for a Custom Post Type
  46. Display Custom Taxonomy Alphabetically
  47. Import data from CSV with custom taxomony empty
  48. WordPress uses url different from defined in rewrite arg of custom post type
  49. Custom Post Type Loop with Query Var
  50. Full Custom Post Type List Organised by two Taxonomies
  51. Is it possible to use have_posts function to list custom post type posts inside a category?
  52. WordPress Doesn’t Generate Taxonomy Archive
  53. URL rewriting for CTP assigned entity
  54. How to have this permalink structure: post_type/postname/custom_inner_page
  55. Suggested Post and Taxonomy structure
  56. How do I require the specification of term in a custom post type and custom taxonomy?
  57. Custom Post, set object Taxonomy terms in plugin
  58. Query Custom Post Type Taxonomy term with multiple parameters
  59. Display post content with respect to its title?
  60. Hierarchy and access control for Custom Post Types (CPT)
  61. Update database from Quick Edit according to Checkbox state of Custom Post Type
  62. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  63. Auto Complete Search
  64. Custom Post Type Pagination Paginates Only in URL Structure
  65. Set post terms for multiple posts
  66. Load Custom_post_type categories post with ajax
  67. Custom Permalink Structure for Custom Post Type, Custom Taxonomy, Under a page
  68. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  69. Custom taxonomy returns 404
  70. Get terms that contain posts that in turn belong to other terms?
  71. WP-API select custom taxonomy when creating a post
  72. Best method to make posts searchable, sortable and filterable – custom field, tag or category?
  73. First custom field value (out of several) displayed twice after query
  74. Custom post with more than one custom taxonomy
  75. Dynamic Custom Fields
  76. How to get custom posts sub category link
  77. Custom while loop for hierarchical display of a taxonomy
  78. Terms showing up in object cache on unrelated pages
  79. Show Custom Post Type meta boxes only on Page Edit
  80. Lists custom taxonomy terms that has specific custom field value assigned to the term (not post)
  81. Get the taxonomy of a post hierarchically
  82. Adding custom taxonomy in same menu place with two custom post types
  83. How to make custom taxonomy into drop down select in a custom metabox
  84. Including taxonomy term before post type breaks top level pages
  85. Correct procedure for advanced permalinks
  86. Permalinks: custom post type -> custom taxonomy -> post
  87. List Taxonomy Terms based on another Taxonomy
  88. How to get the intersection of two terms both from different custom taxonomies
  89. CPT’s with landing page instead of archive – Url Rewrite?
  90. Template for all categories of a custom post type?
  91. How to change permalink to include custom post type
  92. Add Custom Post Type of specific Custom Taxonomy to regularly blog loop
  93. posttype/taxonomy/term archive page 404 error
  94. How to add attributes to taxonomies that may be different from post to post?
  95. Display taxonomy terms, child terms and posts in a template
  96. Custom filter function not working with Custom post type
  97. Custom post type, permalinks, taxonomies and blog posts
  98. How to group posts and get a mixed posts and groups view?
  99. Custom post type permalink tag 404
  100. How to make front end form only accept certain values
Categories custom-post-types Tags custom-post-types, custom-taxonomy, post-meta
How do I nest conditionals to identify sub-site & other criteria?
How to exclude posts by ID within a category/archive loop

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