How do I hide posts across all loops based on the value of a custom field?
I'm trying to dynamically hide posts across my entire site if the value of a custom field called distribution
is "1".
I've set up a meta box on the post editor screen to set the value of the field, but, especially on older posts, the field will not always be set.
Note that I'm doing this in a plugin, so the easy way of directly editing loops and WP_queries in the theme isn't an option for me.
It seems like pre_get_posts
or posts_where
will do what I want.
function hide_custom_filter( $where = '') {
global $wpdb;
if (!is_single() !is_admin()) {
$where .= " AND (($wpdb-postmeta.distribution = '1')) ";
}
return $where;
}
add_filter( 'posts_where', 'hide_custom_filter' );
The above code gives me an error: WordPress database error: [Unknown column 'wp_postmeta.distribution' in 'where clause']
.
What's going on here?
Topic posts-where pre-get-posts custom-field plugins Wordpress
Category Web