I’m trying to only show posts that a user hasn’t seen, like this:
$exclude = array(1,2,3);
$include = array(3,4,5);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 30,
'post__in' => $include,
'post__not_in' => $exclude,
'orderby' => 'post__in',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($category_names),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'ID',
'terms' => array($category_ids),
'operator' => 'NOT IN'
)
)
);
$posts = get_posts($args);
I’d expect this to return posts 4 and 5, but it returns 3, 4, and 5.
How do I set which gets preference between ‘post_in’ and the ‘post_not_in’ parameters?
I know that I could use array_diff() on the $exclude and $include variables, but the reality is more complicated than this question. I have many different arrays to compare against, and some of them are multidimensional. I’m also including and excluding certain taxonomies.
I also think it’s easier for others to read if it’s a WP_Query or $wpdb call, rather than lines and lines of PHP to end up with a query parameter.
Also, if array_diff equal an empty array, then the query actually returns posts, not nothing.
So perhaps the best way is a $wpdb call, or a way to use WP_Query’s meta_query on the post ID field?
post__in
andpost__not_in
are mutually exclusive.As your question is written, the solution you reject– that of using
array_diff
— is the obvious answer.If it is true that that solution won’t work you will need a filter on
posts_where
(probably) that will provide the query logic you need. As you have not explained “the reality [that] is more complicated than this question” it is really not possible to offer a guess at what that logic might be.But here is an example of a
posts_where
filter:Based on the edit to the question and on this statement of intent:
Write the PHP to create an array of posts that the user has seen and exclude them with
post__not_in
. Your attempt to shove everything into the query is only complicating things.By the time your write the filters and or SQL to include everything in the query it will not be more “readable” and it is questionable whether it would be more readable in the first place. You are trying to do this the hard way.
WP_Query
is not capable of complex logic and complex logic in SQl is hard to write and hard the read even on a good day.Excude and include cannot be used together, this code is used in core, so or one or the other.
A $wpdb query is easy if the only arguments you need are only ones you posted.
This way is easy, but if you can’t can explain why?