What do add_filters() and apply_filter() do?

What are you attempting to filter? I’ll assume you’re trying to add a filter to a filter hook called posts_before. In which case, you need to add your filter to this hook, via add_filter():

function mytheme_filter_posts_before( $where="", $date) {

    $where .= " AND post_date < '" . $date . "'";
    return $where;
}
// Add function to the filter hook
add_filter( 'posts_before', 'mytheme_filter_posts_before' );

Note that I changed your function name. filter_posts_before() is far too generic of a function name, and very likely to cause a function-naming collision.

EDIT

And just to clarify:

  • apply_filters() is the filter hook location, is called by core code, and is used to apply any filters that are added to the queue by Themes/Plugins (and core).
  • add_filter() is called by Themes/Plugins (and core), and is used to add filters to the queue to be applied to the hook by core.

EDIT 2

Based on your comment above, the hook is posts_where. So, let’s take a crack at re-building your callback function:

function mytheme_filter_posts_where( $where ) {

    // Here, we need to figure out how to
    // determine what date to use. In your 
    // code example, you call get_the_date(),
    // but this function must be used inside
    // the Loop. Let's try get_the_time()
    // instead. You'll just need to come up
    // with a way to determine what post ID to use.
    $post="some_post_id";
    $date = get_the_time( 'Y-m-d', $post );

    $where .= " AND post_date < '" . $date . "'";
    return $where;
}
// Add function to the filter hook
add_filter( 'posts_where', 'mytheme_filter_posts_where' );

Leave a Comment