[wp-hackers] Posts Before, Posts After

Thomas Belknap dragonfly at dragonflyeye.net
Fri Aug 21 11:46:26 UTC 2009


Perhaps I'm just completely overcomplicating things, but I don't think
so....

I'm trying to get a "contextual archive" list going, where I can show my
visitors the next and previous five posts in a list. I would have thought
this would be easier than it is, but there really isn't any natural way to
do this with query_posts. According to the examples given in the discussion
of query_posts in the Codex, to apply a date range, you need to add a
filter.

The problem with adding a filter is that it applies to ALL future queries,
even if you create your own custom query object, as one person in the
support area discovered. With my code (below), this ends up warping the
query that displays an individual post, thus sending people with the correct
query vars in the URL, but the wrong content on the page.

I'm completely at a loss as to how to go about doing what I want to do. You
can see my (currently borked) code running on my website:
http://iam.bit-character.com. Click on the little plus sign fixed to the
bottom right side of your browser window to see the list of articles.

References:
http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters
http://wordpress.org/support/topic/207832?replies=1*

Code:

function iabc_context_archive($current_post) {
    global $wpdb;
    if(is_single()) :
        // get newer posts:
        add_filter('posts_where', 'iabc_filterWhere_newer');
        $newer = query_posts('showposts=5&sortby=date&sort=DESC');
        remove_filter('posts_where', 'iabc_filterWhere_newer');
        // add_filter('posts_where', 'iabc_filterWhere_reset');
        // get older posts:
        add_filter('posts_where', 'iabc_filterWhere_older');
        $older = query_posts('showposts=5&sortby=date&sort=ASC');
        remove_filter('posts_where', 'iabc_filterWhere_older');
        // add_filter('posts_where', 'iabc_filterWhere_reset');
        // merge returns into single array. Add the current post and mark it
as such:
        if($current_post) :
            $current_post->thepost = 1;
            $newer[] = $current_post;
        endif;
        $list = array_merge((array)$newer, (array)$older);
    else:
        // just get the latest posts
        $list = get_posts('numberposts=5');
    endif;
    // Output:
    print('<ul id="archive">');
    foreach((array)$list as $next) :
        $title = str_split($next->post_content, 40);
        $next->thepost ? $class = ' class="current" ' : $class = '';
        print('<li'.$class.'><a href="'.get_permalink($next->ID).'"
title="'.$title[0].'">'.$title[0].'</a></li>');
    endforeach;
    print('</ul>');
        add_filter('posts_where', 'iabc_filterWhere_reset');
}

function iabc_filterWhere_newer($where = '') {
    global $post;
    $where .= " AND post_date > '".$post->post_date."'";
    return $where;
}

function iabc_filterWhere_older($where = '') {
    global $post;
    $where .= " AND post_date < '".$post->post_date."'";
    return $where;
}


function iabc_filterWhere_reset($where = '') {
    $where = "";
    return $where;
}

*


More information about the wp-hackers mailing list