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

Excluding posts by meta, and also keeping posts without the meta

I think I managed to solve this.

What I did was to add the following in my functions.php file:

add_filter( 'posts_where', 'exclude_my_posts' );    

function exclude_my_posts( $where ) {
    global $wpdb;

    //  For the front page. Hide posts with "efter15" set to "Ja", and only show posts with "show_on_front_page" set to "Nej"
    if (is_front_page()) {
        return $where . " AND $wpdb->posts.ID NOT IN ( SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'efter15' AND meta_value="Ja" ) AND $wpdb->posts.ID NOT IN ( SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'show_on_front_page' AND meta_value="Nej" )";
    }
    //  Always show all in dashboard
    elseif (is_admin()) {
        return $where;
    }
    //  Everywhere else: Hide posts with "efter15" set to "Ja"
    else {
        return $where . " AND $wpdb->posts.ID NOT IN ( SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'efter15' AND meta_value="Ja" )";
    }

}

And in my index.php file – which is also used for category, tag etc (therefore the is_front_page()) for now at least:

<?php if (is_front_page()) {
    query_posts(array('category_name' => 'notiser', 'paged' => get_query_var('paged')));
} ?>

Then follows the loop and the rest. Looks good so far.

Related Posts:

  1. Adding an orderby filter, casting postmeta with multiple keys
  2. Use REGEXP in WP_Query meta_query key
  3. Query WP REST API v2 by multiple meta keys
  4. How to hook a filter to catch get_post_meta when alternate a custom field output?
  5. How to use update_{$meta_type}_metadata filter to modify meta value
  6. What is the most efficient way of querying posts based on visits and date for current day?
  7. Custom Queries: Joining On Meta Values From Two Custom Post Types
  8. How can I display image metadata?
  9. Possible to filter the posts or categories that XML-RPC users see in their mobile application?
  10. How to display liked posts of current user in wordpress?
  11. How to filter into post meta data before saving
  12. failed to filter hook `get_terms_defaults`
  13. Filter custom post types in archive
  14. Explanation for remove_filter used in the below code [closed]
  15. Filter list by a unique meta value dilemma
  16. How to remove get_post_metadata using remove_filter inside a class?
  17. Sorting and limitation with pre_get_posts
  18. Query metas (and not : query posts by metas)
  19. Filter Posts by current Month
  20. Query posts if meta key starts with
  21. Custom wp_query time filter on meta_value
  22. Is it possible to apply filter to meta key value when querying posts?
  23. Order by meta value pro first then meta value free in my search function
  24. Filter for author list in gutenberg core editor
  25. Filter posts by meta data using custom query
  26. Want to use wp_get_current_user() in query filter
  27. get_post_meta with WP_query
  28. WP_Query meta compare must include ALL array values
  29. Customising the default wordpress search functionality
  30. WP_Query filter by custom meta
  31. List all categories that have results in a query
  32. Display posts from today and future in Elementor ‘posts widget’
  33. Query against multiple locations within single custom post type post
  34. Re-order search results with posts_orderby filter and post meta value
  35. Having issue with dynamic data within restrict_manage_posts function
  36. Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
  37. Displaying posts on a page that have been assigned a value in a database
  38. Add option to query string before get_posts() is called on archive.php
  39. Filter on the day of the week from timestamp
  40. Admin view: filter posts by whether they have specific taxonomy attached
  41. How to sort posts according to meta value?
  42. Restrict access to custom post type and filter from every query
  43. How to add more than one custom metadata as filter on the post list page?
  44. What is the best way to query posts based on live data?
  45. meta query always fails even if data is correct
  46. Hide H1 Title using the_title filter
  47. Query Multiple Filters, one with Meta
  48. How to use parse_query() to add an additional filter based on content to page search?
  49. Disable emojicons introduced with WP 4.2
  50. How to remove a filter that is an anonymous object?
  51. WordPress hooks/filters insert before content or after title
  52. How to add defer=”defer” tag in plugin javascripts?
  53. How can I create a meta_query with an array as meta_field?
  54. add_action(), add_filter() before or after function
  55. apply_filters(‘the_content’, $content) vs do_shortcode($content)
  56. order by numeric value for meta value
  57. How do filters and hooks really work in PHP
  58. Trouble understanding apply_filters()
  59. What is the very earliest action hook you can call?
  60. How would one modify the filtering Gutenberg applies to pasted content?
  61. How can I modify the WordPress default widget output?
  62. Add custom options to the wplink dialog
  63. Remove classes from body_class
  64. what is __return_false in filters
  65. Explanation for apply_filters function and its variables
  66. Gutenberg: Is there a way to know if current block is inside InnerBlocks?
  67. How to reorder billing fields in WooCommerce Checkout template? [closed]
  68. Insert HTML just after tag
  69. the_content and is_main_query
  70. Changing WooCommerce Display Price Based on User Role & Category [closed]
  71. How to add a custom CSS class to core blocks in Gutenberg editor?
  72. How to show page content in feed?
  73. wp_headers vs send_headers. When to use each?
  74. Filter any HTTP request URI?
  75. How to Pass External Variables to Filters/Actions
  76. How to filter users on admin users page by custom meta field?
  77. Filter by one custom field, order by another?
  78. Not able to change wp_title using add_filter
  79. How to appending to the_content using add_filter with custom post type?
  80. No filter of code on switch from html to visual editor, how?
  81. Sanitize and data validation with apply_filters() function
  82. How to modify posts_where filter only for the search query
  83. How to get shortcode’s input values inside a filter?
  84. Removing Image and Caption Dimension Attributes
  85. How to wrap oEmbed-embedded video in DIV tags inside the_content?
  86. How to bulk delete all users with no posts?
  87. How many filter/action hooks are healthy?
  88. WordPress 3.9 – Trouble Editing TinyMCE 4.0
  89. Changing JPEG compression depending on image size
  90. How to add filter with 2 args?
  91. Why is javascript allowed in my post content?
  92. How to wrap an element around an iframe or embed in content automatically?
  93. Filter specific shortcode output?
  94. WordPress Internal @ Mentions
  95. How to add headers to outgoing email?
  96. Earliest hook to reliably get $post/$posts
  97. Insert new element to array with add_filter
  98. LESS CSS enqueue_style with add_filter to change rel attribute
  99. Is it possible to filter comments in a post so a user can only see the comments they have written?
  100. How to pass/get data to/from the WooCommerce data-product_variations object?
Categories filters Tags filters, meta-query, post-meta, query
Query not sorting DESC
Need equivalent of single_term_slug

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