This looks straightforward, but unfortunately isn’t, because wp_query
doesn’t allow you to exclude posts on basis of multiple arguments (in this case category and date). You can select on basis of multiple arguments, but not exclude. So, to do what you want, you must in some way bundle your two arguments into one. Conceptually, you select the posts you do not want and then use that list to exclude them on basis of their ID’s. Here we go:
add_action ('pre_get_posts', 'wpse308323_multiple_exclude', 10, 1);
function wpse308323_multiple_exclude ($query) {
// only do this on home page for main query
if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) {
// define posts to exclude and get them
$args = array (
'category_name' => 'third-party',
'date_query' => array(
array(
'before' => '2 weeks ago'
)
)
);
$q2 = new WP_Query ($args);
// extract an array of ID's from the posts retrieved in $q2
$q2_ids = wp_list_pluck ($q2->$posts,ID);
// exclude these ID's from the main query
$query->set ('post__not_in', $q2_ids);
}
}
Disclaimer: above code is given to show the concept. I haven’t tested it, so debugging may be necessary. Some references:
- How
pre_gest_posts
works - Valid date queries, also see
strtotime
(this can get pretty complicated) - How
wp_list_pluck
works