• OK, I think I understand the nature of the problem, but not the solution.

    I’m trying to create a contextual archive list: the next five posts and the previous five posts after the current post a visitor is viewing. Since there is no native WP function that will do this, I tried using query_posts to accomplish this task in the following way:

    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=data&sort=DESC');
    		remove_filter('posts_where', 'iabc_filterWhere_newer');
    		// get older posts:
    		add_filter('posts_where', 'iabc_filterWhere_older');
    		$older = query_posts('showposts=5&sortby=data&sort=ASC');
    		remove_filter('posts_where', 'iabc_filterWhere_older');
    		// 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>');
    }
    
    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;
    }

    The problem I am finding is that the links don’t work with this active. They go to the correct URL, but at that URL, WordPress seems to be pulling the wrong content: instead of grabbing post ID 21, it seems to be pulling post ID 12, which is the last published article on the site.

    I take this to mean that the posts_where is not getting reset from my last operation, even though I am removing the filter. I have no idea how to correct this problem, and would be very thankful for anyone who can provide a solution!

    An example of the problem can be seen here:
    https://iam.bit-character.com/archives/21
    Click on the plus sign fixed to the bottom right side of your browser window to see the list.

  • The topic ‘Resetting Results of query_post’ is closed to new replies.