Title: filtering posts
Last modified: August 18, 2016

---

# filtering posts

 *  [nateomedia](https://wordpress.org/support/users/nateomedia/)
 * (@nateomedia)
 * [19 years, 4 months ago](https://wordpress.org/support/topic/filtering-posts/)
 * For my plugin to work right, I need to be able to filter out posts when the WordPress
   loop is called. My plugin is called before the loop, which will set the conditions
   for what should be filtered.
 * add_filter does not seem to be the right fit for the job. Using add_filter causes
   filtering to occur EVERYWHERE even if my plugin is not called. add_filter also
   filters results before the loop, which does not allow my plugin the chance to
   decide what needs to be filtered out.
 * I’ve been banging my head against the wall on this one for a couple of weeks 
   now. I’m at a loss. Suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [19 years, 4 months ago](https://wordpress.org/support/topic/filtering-posts/#post-505774)
 * Actually, add_filter() would fit in here quite well, but it’s all about _which_
   filter you hook into.
 * Check out the source for my Custom Home plugin for how you might go about filtering
   out posts *from* the Loop, and also how you can limit when it is put into action:
 *  [http://wordpress.org/support/topic/93438#post-470202](http://wordpress.org/support/topic/93438#post-470202)
 *  Thread Starter [nateomedia](https://wordpress.org/support/users/nateomedia/)
 * (@nateomedia)
 * [19 years, 4 months ago](https://wordpress.org/support/topic/filtering-posts/#post-506063)
 * Thanks for the reply, but this isn’t quite what I need. Your plugin relies on
   knowing what data to filter before the page loads. I need to be able to decide
   what data to filter as the page is loading. That is, the most important test 
   will be: has my plugin been called? If it’s been called, alter the data; if it
   hasn’t been called, don’t alter the data.
 * add_filter seems to get processed before anything gets called on a template. 
   So, if my plugin is called on the template, nothing that I do within my plugin
   will effect the add_filter function that I set up. I tried setting up a global
   variable and was losing my mind trying to understand why it wasn’t working. I
   echo’d the variable and the modified $where statement that was supposed to be
   happening through add_filter and could not understand why my variable was showing
   up on it’s own but not in the function I had set-up through add_filter… until
   I realized that the add_filter had
    happened a long time ago. 🙂
 * add_filter cannot, it seems, be called from within a function either. So, I can’t
   have my plugin get called and then have add_filter run then. That would work 
   fine! But it’s not possible. 🙁
 * What needs to happen:
 * page loads -> plugin called? -> yes -> grab IDs of posts to display, eliminate
   all others -> run loop
 * page loads -> plugin called? -> no -> run loop
 * The solution, I’ve discovered, is to use $wp_query->posts and, in my particular
   instance, was incredibly easy to implement. I was already using $wpdb to make
   a database call like this:
 * `$all_posts = $wpdb->get_results("SELECT post_title, post_content, post_excerpt,
   ID, post_password, post_date_gmt FROM $wpdb->posts, $wpdb->post2cat WHERE post_id
   = ID AND post_status = 'publish'");`
 * If the plan is for the post loop to be effected by the outcome of that database
   call anyway, this works just as well (and solved my problem):
 * `$all_posts = $wp_query->posts;`
 * Just pass your results back to $wp_query->posts when you’re done.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [19 years, 4 months ago](https://wordpress.org/support/topic/filtering-posts/#post-506109)
 * For people looking for an answer _future-tense_, it’s good to point out you’re
   also seeking help, and perhaps solving the issue, elsewhere:
 *  [http://comox.textdrive.com/pipermail/wp-hackers/2007-January/010458.html](http://comox.textdrive.com/pipermail/wp-hackers/2007-January/010458.html)
 * But I’m glad you figured out a solution.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘filtering posts’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * 3 replies
 * 2 participants
 * Last reply from: [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * Last activity: [19 years, 4 months ago](https://wordpress.org/support/topic/filtering-posts/#post-506109)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
