WordPress.org

Forums

filtering posts (4 posts)

  1. nateomedia
    Member
    Posted 8 years ago #

    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?

  2. Kafkaesqui

    Posted 8 years ago #

    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

  3. nateomedia
    Member
    Posted 8 years ago #

    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.

  4. Kafkaesqui

    Posted 8 years ago #

    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

    But I'm glad you figured out a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic