Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm a bit confused as to why this isn't working - although it must be said I'm not that sure what apply_filters() and add_filter are doing, so any general tips would be great too!

I want a query that brings up the five earlier posts on a single post page. I am sending the current post's date off, and want to apply a filter that filters out posts earlier than this.

function sw_filter_posts_before( $where = '', $date) {

    $where .= " AND post_date < '" . $date . "'";
    return $where;
}

How do I correctly apply this? Simply using add_filter or apply_filter before instantiating a new WP_Query object doesn't seem to work correctly.

Thanks in advance!

Edit: To go into things further, I would like to understand how to pass a variable into the filter, as I can't get $date to pass from another function.

Here is said other function (it is an ajax call within wordpress, hence I start by getting the post ID for the current page through a $_POST variable):

function create_more_videos_sidebar() {

    $id = $_POST['theID'];

    $args = array(  'post_type'         =>  'videos',
                    'posts_per_page'    =>  1,
                    'p'             =>  $id
                ); 

    $wp_query = new WP_Query($args);

    while ($wp_query->have_posts()) : $wp_query->the_post(); $do_not_duplicate = $post->ID;

        $date = get_the_date('Y-m-d');

    endwhile;

    $args = array(  'post_type'         =>  'videos',
                    'posts_per_page'    =>  5
                ); 

    add_filter( 'posts_where', 'sw_filter_videos_before' ); //don't know how to pass $date


    $wp_query = new WP_Query($args);
    remove_filter( 'posts_where', 'sw_filter_videos_before' );

    //do loop stuff

    $response = json_encode( array( 'result' => $result ) );

    header( "Content-Type: application/json" );
    echo $response;

    exit;
}
share|improve this question
    
Okay, first things, first: to what hook, exactly, are you trying to add your filter? That matters, because your $date variable uses $post data, so we need to know if we're filtering inside or outside of the Loop. –  Chip Bennett Aug 23 '11 at 14:31
    
I think this might be where my lack of wp filters knowledge is striking me down. I thought the hook was 'posts_where', and it would add the given string to the SQL query. As far as I'm aware that's outside of the loop, as it's in the preprocessing part of creating a loop. Do you have a suggestion on how I would achieve my goal with best practice? –  Michael Watson Aug 23 '11 at 16:27
    
Sure, we can get there. :) What date do you want to use? Asked more accurately: how do you want to determine what date to use? –  Chip Bennett Aug 23 '11 at 17:53
add comment

1 Answer

up vote 3 down vote accepted

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' );
share|improve this answer
    
Thanks for the clarification! I'll make sure to rename the filter. I still don't understand how to pass the $date variable to this, could you clarify that, too? Thanks! –  Michael Watson Aug 23 '11 at 12:34
    
What is the $date variable, and where is it coming from? –  Chip Bennett Aug 23 '11 at 12:38
    
$date is coming from another function of mine, it is the date of the current post, in the format 'Y-m-d'. –  Michael Watson Aug 23 '11 at 12:49
    
Are you globalizing $date, or returning it from the function, what? Can you post that function's code, also? –  Chip Bennett Aug 23 '11 at 13:31
    
Okey-cokey, I've updated the original post with some more details. I could globalize date, but is that entirely necessary? Is it not best, if possible, to just pass it over somehow? –  Michael Watson Aug 23 '11 at 13:42
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.