edit: Think I managed to figure it out, check the answer right under here, or click the following link: https://wordpress.stackexchange.com/a/51175/15809
I've been trying to find a way to exclude posts with 2 meta keys + values, but also keep the posts without the metas at all.
I looked into parse query request and have this in my functions.php
:
function filter_out_efter15_posts( $request ) {
$dummy_query = new WP_Query();
$dummy_query->parse_query( $request );
if ( $dummy_query->is_home() || $dummy_query->is_category('notiser') ) {
$request['meta_query'][0]['key'] = 'efter15';
$request['meta_query'][0]['value'] = 'Nej';
$request['meta_query'][0]['compare'] = '=';
$request['meta_query'][0]['type'] = 'CHAR';
$request['meta_query'][1]['key'] = 'show_on_front_page';
$request['meta_query'][1]['value'] = 'Ja';
$request['meta_query'][1]['compare'] = '=';
$request['meta_query'][1]['type'] = 'CHAR';
return $request;
}
else {
return $request;
}
As you can see above, queries on the front page/home and on the category notiser (only category with posts that have these metas) filters out any posts with the meta key efter15
with the value Nej
, and meta key show_on_front_page
with the value Ja
.
However, when I go to a tag page, the above function will not alter anything, and thus all posts with that tag will show up. I still want the posts with the metas filtered out.
I could add $dummy_query->is_tag()
, but then it would filter out all the other posts that does not have the metas. Obviously I cannot set specific tags, like with the category.
Hopefully you'll understand what I'm trying to do. Is there a clean and good way to deal with this? I'd like to keep the number of queries to a minimum, and there are 20k+ posts in the database so I don't know about running all of them into post__not_in
, read it might slow things down a bit.
Do I have to use a custom select query?
Thanks