Title: posts_where filter is ignored
Last modified: August 20, 2016

---

# posts_where filter is ignored

 *  [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/)
 * Hello Folks,
    i have a short question, as i am new to filter, this might be easy
   for you , but it is very strange to me, so many somebody has some input for me?
 * I want to exclude some posts from the WP search, and let us say i really need
   to manipulate the sql query for that.
 * i tried a lot to change the “$GLOBALS[‘wp_query’]->request” witch seams to contain
   
   the search query with no success, until i found the posts_where filter.
 * my code:
 *     ```
       function se_postStrip ($where) {
       	global $wpdb;
       	if ( is_search() && isset($_GET['s'])) {
       		$where .= "AND ".$wpdb->posts.".ID is not in (1,2,3)  ";
       		echo $where;
       	}
       	return $where;
       }
   
       add_filter ('posts_where', 'se_postStrip');
       ```
   
 * seams to work , as the echo delivers a perfect sql statement.
    BUT the posts 
   with the ID 1,2 and 3 are still in the search result.
 * I put the code in the functions.php and in the index.php of my theme, but it
   
   still results me the posts 1,2,3
 * it feals like, i change the query to late … but i have other filters in the same
   file – and they work… so what am i doing wrong ?
 * What can i do that the filter executes me the additional where statement ?
 * Best regards and thanks a lot !
 * p.s.
    To keep it easy i changed the where statement to “is not in” and i know
   that there are other ways to exclude IDs , but as i said this is just an example
   to keep it easy.
 * My real where statement its a more complicated, but if i get the “is not in” 
   to run, then i can go on myself.

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/posts_where-filter-is-ignored/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/posts_where-filter-is-ignored/page/2/?output_format=md)

 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969545)
 * Hey folks,
    me again.
 * Ok even if i make a clear sql error in the statement, i get a result…
 *     ```
       $where .= " AND ".$wpdb->posts.".IDXAXAXAXA is in (1,2,3)  ";
       ```
   
 * so thats at least the prove that my filter is ignored… but why ?
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969608)
 * Try doing a print_r($where); at the top of the function. Is it printing out SQL?
 * The filter I prefer to user is posts_request, You get more access to the query
   in this filter.
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969615)
 * Hi Eric,
 * yes it does.
 * If i echo, or print_r $where i get the where statement matching to my search 
   criteria.
    after that i add my personal “and” statement.
 * So that looks fine.
 * i also checked that surpress_filters is not active and checked that the filter
   its used
    in the ./wp-includes/query.php all looks good – but it has no impact
   on the result…
 * as i said – i can write total nonsense in the $where, and the search is triggered
   
   as if i nothing happens.
 * thats why i think that i put the trigger to late, and the query is already executed,
   
   is this possible ?
 * i checked the posts_requests filter, and that is also funny.
 * i inserted that code in my functions.php
 *     ```
       function my_posts_request_filter( $input ) {
       	print_r( $input );
       }
       add_filter( 'posts_request', 'my_posts_request_filter' );
       ```
   
 * and i get
    “SELECT * FROM wp_posts WHERE 1=2”
 * so this does’t look like my query 😀
 * and also here if i change that code to this:
 *     ```
       function my_posts_request_filter( $input ) {
       	global $wpdb;
       	$input = "SELECT * FROM wp_posts where ".$wpdb->posts.".ID is  in (1,2,3) ";
       	print_r( $input );
       }
       add_filter( 'posts_request', 'my_posts_request_filter' );
       ```
   
 * it has no impact on my search result. I get the same posts, as if this filter
   would not be in the code.
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969621)
 * One thing to try with your posts_where clause is upper-casing IS NOT IN. I’m 
   not sure if the posts query does a prepare or not, perhaps it is not reading 
   it as proper SQL because of lowercase
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969624)
 * My mistake, the filter I MEANT to give you was posts_clauses, not posts_request.
   It should give you the entire query array.
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969631)
 * Hi Eric,
 * on your first post. No i think case sensitivity is not the problem. I found nothing
   in the query.php that looked like an “sql validator” and i can also write
    ” 
   AND cgvjhkblöhlgkcjfcgkjhljö” and nothing happens.
 * on your second post:
    thanks for the posts_clauses filter , its good to have 
   all parts in one filter, but same result here.
 * it just doesn’t impact the search.
 * i have this code now:
 *     ```
       function se_postStrip ($where) {
       	global $wpdb;
       	if ( is_search() && isset($_GET['s'])) {
       		$where .= " AND ".$wpdb->posts.".ID IS IN  (1,2,3)  ";
       	}
       	return $where;
       }
   
       add_filter ('posts_where', 'se_postStrip');
   
       function my_posts_request_filter( $input ) {
       	global $wpdb;
       	print_r( $input );
       }
       add_filter( 'posts_clauses', 'my_posts_request_filter' );
       ```
   
 * and in the **print_r** of the **posts_clauses** filter , i see that the additional
   where statement was added in the **post_where** filter, but i still get post 
   ID 4747 in the search result,
    and 4747 is NOT IN (1,2,3) 😀
 * so no look until now
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969632)
 * Can you copy in the entire posts_where value once it’s altered by you?
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969636)
 * sure. here you have the print_r ($input) from the post_clauses filter:
 *     ```
       Array (
       [where] => AND (((wp_posts.post_title LIKE '%sports%') OR (wp_posts.post_content LIKE '%sports%'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND wp_posts.ID IS IN (1,2,3)
       [groupby] =>
       [join] =>
       [orderby] => wp_posts.post_date DESC
       [distinct] =>
       [fields] => wp_posts.*
       [limits] => LIMIT 0, 7 )
       ```
   
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969637)
 * print_r($where); from post_where filter says
 * `AND (((wp_posts.post_title LIKE '%sports%') OR (wp_posts.post_content LIKE '%
   sports%'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.
   post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status 
   = 'private') AND wp_posts.ID IS IN (1,2,3)`
 * but this is not surprising 😀
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969639)
 * Ahhh, yes.. Attempt removing “IS” in “IS IN”. Looking at that current post, I
   see:
 * AND wp_posts.post_type IN (‘post’, ‘page’, ‘attachment’)
 * I am thinking “IS” is not proper SQL syntax.
 * Give that a shot!
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969641)
 * good point, but nope…
    i tried to make it more direct now
 *     ```
       Array (
       [where] =>
       AND (((wp_posts.post_title LIKE '%sports%') OR (wp_posts.post_content LIKE '%sports%'))) AND wp_posts.post_type IN ('post', 'page', 'attachment')
       AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1
       AND wp_posts.post_status = 'private')
       AND wp_posts.ID = 1
       [groupby] =>
       [join] =>
       [orderby] => wp_posts.post_date DESC
       [distinct] =>
       [fields] => wp_posts.*
        [limits] => LIMIT 0, 7 )
       ```
   
 * still got the same search result, with all posts that contain “sports”
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969643)
 * AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_author = 1
    AND wp_posts.
   post_status = ‘private’)
 * should read
 * AND (wp_posts.post_status = ‘publish’
    OR (wp_posts.post_author = 1 AND wp_posts.
   post_status = ‘private’))
 * Other than that, I’m at a loss.
 *  Thread Starter [derschwede](https://wordpress.org/support/users/derschwede/)
 * (@derschwede)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969645)
 * jeep maybe , but all of them should be targeted by the last stand alone
 * “AND wp_posts.ID = 1”
 * so in fact not one single post , is valid if it is not ID: 1
 * the trigger isn’t used… thats my point, the syntax is correct,but it is just 
   ignored
    and i have no clue why…
 * i am going to search deeper in the query.php , but i do not want to make changes
   here,
    to keep the system up-dateable.
 * thankx for your time bro – i appreciate that – but maybe somebody ready that 
   who knows why the filter just does nothing 😀
 *  [Eric Holmes](https://wordpress.org/support/users/ew_holmes/)
 * (@ew_holmes)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969646)
 * One other method you could use (not as clean) is do the filtering right within
   the loop on search.php
 * something like:
 *     ```
       <?php
       $array_of_posts_to_exclude = array( 1, 2, 3);
   
       while (have_posts()) :
       the_post();
       if( ! in_array( the_ID(), $array_of_posts_to_exclude ) ) : ?>
   
       <div class="post">
       <?php the_content(); ?>
       </div>
   
       <?php
       endif;
       endwhile; ?>
       ```
   
 *  [danielmartins](https://wordpress.org/support/users/danielmartins/)
 * (@danielmartins)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/#post-2969863)
 * I have the same problem here. My filters callback on posts_where not working 
   on search query. No clue about why.
 * [@derschwede](https://wordpress.org/support/users/derschwede/), you figure out
   this problem ?

Viewing 15 replies - 1 through 15 (of 17 total)

1 [2](https://wordpress.org/support/topic/posts_where-filter-is-ignored/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/posts_where-filter-is-ignored/page/2/?output_format=md)

The topic ‘posts_where filter is ignored’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 17 replies
 * 3 participants
 * Last reply from: [danielmartins](https://wordpress.org/support/users/danielmartins/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/posts_where-filter-is-ignored/page/2/#post-2969865)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
