Why not work post__not_in?

There are posts, there are pages. and so and so set the tags (to specify tags for the page, put the plugin Simple Tags).
Need to get ONLY posts with a specific tag, not including the current post (excluding, because the conclusion in the post). here is an example code (here ID of the tag and the post is inserted explicitly, just so as not to look for errors in obtaining these ID):
$related_args = array(
 'category_name' => 'blog',
 'tag__in' => array(137),
 'numberposts' => 10,
 'post__not_in' => array(19222)
);

 $related_posts = get_posts($related_args);

var_dump($related_posts);

 wp_reset_postdata();


the arguments you pass in an id tag in which to search for relevant posts and the current post id to exclude him from extradition. this code displays posts but not excluding the current one. clean category_name, post is excluded, but the results is added to the type "page". ask 'post_type' => 'post' does not help.
April 4th 20 at 13:21
2 answers
April 4th 20 at 13:23
Solution
function category_filter($query) {
 if ($query->is_category) {
 $e_post_id = get_id_by_slug('write-for-us');
 $excludeId = array($e_post_id);
 $query->set('post__not_in', $excludeId);
 $query->set('post_type', array('page', 'post'));
}
 return $query;
}

add_filter('pre_get_posts', 'category_filter');

here's the code that broke everything
April 4th 20 at 13:25
'post_not_in' => get_the_ID()
you got two errors: post__not_in from two underscores, + array is passed here
and no, it doesn't work - harry36 commented on April 4th 20 at 13:28
@harry36, Saran)) coffee talk now drank

'post__not_in' => array( $post->ID ), - randy_Schultz commented on April 4th 20 at 13:31
@randy_Schultzdoesn't work, still displays the post with this ID - harry36 commented on April 4th 20 at 13:34
@harry36, I works great. You're not outside the loop with your custom loop insert? - randy_Schultz commented on April 4th 20 at 13:37
@randy_Schultz, outside the loop. get_posts because it allows out-of-cycle insertion. in my code there is no $post->ID , and the ID of the post you want to exclude, I found out through the admin panel. in General, it's not that ID is incorrect, but the fact that the sample still displays on the post you want to exclude. without the category_name is displayed without it - harry36 commented on April 4th 20 at 13:40
@harry36, XS, I within a cycle is displayed without fasting completely with any options. True, I yuzayu class, not get_posts. - randy_Schultz commented on April 4th 20 at 13:43
WP_Query does not exclude post too. before everything also worked as it should, I noticed today that no longer correctly displayed. looking for that that might affect. I don't see any plugins that can affect it, or global variables - harry36 commented on April 4th 20 at 13:46

Find more questions by tags WordPress