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

Is there a way to exclude posts based on meta_values?

I met the same problem before. I tried to use meta_query parameter in the query argument, but didn’t succeed. The code I tried to use looks like this:

$software = array(
    'post_type' => 'software',
    'meta_query' => array('key' => 'legacy', 'value' => 1, 'compare' => '!=')
);
query_posts($software);

But the problem is this query also excludes posts that don’t have that meta key.

So I had to use a custom query, that gets all posts with legacy meta key, and then excludes them:

global $wpdb;
$legacy = $wpdb->get_col("SELECT ID FROM $wpdb->postmeta WHERE meta_key='legacy'");

$software = array(
    'post_type' => 'software',
    'post__not_in' => $legacy
);
query_posts($software);

Hope this can help you.

Related Posts:

  1. Correctly delete posts with meta and attachments [duplicate]
  2. Storing revisions of metadata for custom post type
  3. Custom Post type – how to get checkbox to update meta field to null
  4. How do I Paginate Search Results for Custom Post Types?
  5. Ordering posts having multiple post-meta date fields
  6. Custom Post Type with Input fields to seperate table on database.
  7. How to programmatically create a connection with [Plugin: Posts 2 Posts] on cpt publish?
  8. How Can I save multiple records in same meta key?
  9. Permalinks using event date (year & month) instead of publication date
  10. How Do I Use WP_Query to Run This Database Query as Search Result?
  11. Creating a custom post type upon registration
  12. Reason action hook won’t work with update_post_meta from frontend form? Alternative?
  13. Showing Meta Data for Custom Post Types?
  14. Having trouble with custom post type / meta box
  15. Query for posts from any post type but only add instock products
  16. update custom post type meta from a shortcode
  17. Custom post type category not displaying in custom post type
  18. How to make custom post meta wp_editor translatable?
  19. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  20. How to add multiple featured image in meta box in post editor?
  21. Show metabox in custom-post-template depending on taxonomy term?
  22. ACF Upload Image in repeater from front-end with custom form? – add_post_meta()
  23. update_post_meta() whenever custom post type is updated
  24. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  25. Using date stored as custom field to filter posts displayed in admin
  26. Meta Box Only in CPT Edit Page, Not in Add New
  27. get_template_part() – post-meta not working?
  28. Count posts with specific term_meta
  29. Custom search for custom post meta with pre_get_posts interferes with WP search
  30. My theme saves their custom post type’s metadata as a serialized array, how to access the keys?
  31. Does WordPress limit the length of slug names for Post Meta or Terms?
  32. Populate Custom Fields in a Custom Post Type?
  33. Insert Multiple Post with Same Publish Button
  34. Custom meta box data array: foreach not working correctly?
  35. Undefined Variable – Custom Post Type Meta
  36. How to check if user meta field is empty in conditional else statement
  37. Post image in WordPress not appearing on home page
  38. changing meta value and meta key of price field
  39. Posting to a Custom Post Type from front end – user generated content
  40. Getting meta in editor plugin, and event triggering issue
  41. Should wp_postmeta meta_key always be unique for a given post_id?
  42. Metabox not show in categories custom post type cmb2
  43. Can’t publish custom post type – “You are not allowed to edit this post.”
  44. Filter date from post meta when date is in string format
  45. Get only used meta_values
  46. Submitting Custom Post Types with custom fields from Front-end form
  47. create custom meta box with default value
  48. How to Echo Metadata Value in Currency Format
  49. Issue on Working with Metabox – Checkbox to enable Sale Items
  50. Filter posts by tax (dropdown) and meta value
  51. Publish and save specific postmeta to a filtered post_type
  52. Batch Extract Date from post title and put into ACF custom field
  53. How to handle this specific case of custom post type?
  54. Save CTP post meta different values each day without overwriting previous values
  55. get_post_meta returns NULL in front-end, but correct result in back-end
  56. Meta box data is saved but NOT displayed in the meta box text field. Why?
  57. How to call a post’s metadata in shortcode.php?
  58. Echo custom post meta from options array
  59. Value of post meta dropdown is not showing in WordPress
  60. Sanitaizing Select Optin For Custom Post Type Metabox in WP
  61. HM CMB: Post Select Field for CPT ID
  62. Getting WordPress to store 0 values for custom post type meta
  63. Problem Saving Custom Post Type Meta Values
  64. Should I use a custom taxonomy or custom post type for grouping a list of panels associated with a group of tests?
  65. Get_post_custom not fetching value from array wordpress
  66. Save data is post using php front end
  67. Creating a custom post type upon registration for a specific user role
  68. How to sort by multiple values in a nested WP_Query
  69. Peepso: Add extra data field (custom post meta) to postbox
  70. Where is get_post_meta value located?
  71. How do I create a customised table list of a custom post type (in the admin area)?
  72. Change picture attachment meta of all occurrences of a picture
  73. Unknown Post Meta Records
  74. add action save post type and update post meta woocommerce
  75. get_post_meta not working with variable as a post_id for dynamically get the postid
  76. How to save post_status using action save_post?
  77. Custom post meta box as a sub form
  78. Metaboxes in Loop
  79. Add more custom fields when creating a new custom post type
  80. Custom meta box data not saving
  81. Collect custom post in a calendar
  82. Storing data in a multidimensional array from dynamically generated foreach loop
  83. Disable Facebook Comments Automatically On CPT
  84. Problem with ‘save_post’ hook not running
  85. WordPress custom post type
  86. Edit post meta direct from post.php?
  87. Howto: use existing post_meta as options for a different metabox (checkboxes or list)
  88. Values inside a custom field to determine which category posts to display
  89. Timetable of Custom Meta Data using Custom Post Type and Custom Taxonomy
  90. How to rewrite CPT-Permalinks the correct way (incorporating meta-box-data)?
  91. Change the contents of a dropdown through the admin panel?
  92. Advanced search form with filters for custom taxonomies and custom fields
  93. Top 30 Songs using Custom Post Type
  94. Advice Needed for Post Meta Database Efficiency
  95. Querying meta values within an array
  96. why get_post_meta is returning 0?
  97. Save post_parent in a custom post type
  98. Use a Variable in update_post_meta as the $meta_key
  99. Update an existing post in a custom post type
  100. getEntityRecords/useEntityRecords: How to use CPT metadata?
Categories custom-post-types Tags custom-post-types, post-meta
Great blogs site resources/tutorial for WordPress Developers? [closed]
WordPress MU subdomain vhost

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