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?
AND (pm.meta_key = 'distribution' AND pm.meta_value != '1')
(pm for shortening my code here, use wpdb->.. instead). Still not sure if this SQL works. What if a post does not have this meta key? Anway, the linked answer should solve this