• Resolved ianfulgar

    (@ianfulgar)


    The Loop (WP_Query) always retrieves results starting with the most recent post. Is there a way to offset the results (a list of the posts) starting at a particular post_id instead of starting from the most recent post? For example, showposts=10 but start at post_id = $this_post_id?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The template tag, query_posts(), offers the offset argument.

    Thread Starter ianfulgar

    (@ianfulgar)

    Hello MichealH. Thanks for the reply. But the query_posts(offset=number) always begin the count from the most recent post. Is it possible to offset to a particular post ID? For example if I want to retrieve all posts starting from post ID = 67 (but NOT necessarily offset to the 67th post starting from the most recent post), what is the best way to approach this?

    Are you saying that actually the number provided after offset equals to actually pertains to the post ID and not the ordinal number of the query result (e.g. 2nd, 3rd, 4th, etc.)?

    No the offset does not pertain to the post ID, but I was just making sure you knew of the offset.

    Guess you could create a filter that checks for Post IDs, or return a query for all posts then in your loop, skip any posts that have a post ID less than 67.

    Didn’t test this but maybe this filter will work:

    <?php
    function filter_where($w = '') {
            $w .= " AND ID > 67";
            return $w;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    //remove_filter('posts_where', 'filter_where');
    ?>

    Thread Starter ianfulgar

    (@ianfulgar)

    Ah yes, I see what you mean. This could work. Thank you for introducing add_filter(). Hardly remember ever using that function. I’ll try test things out and share my findings.

    Thread Starter ianfulgar

    (@ianfulgar)

    I played with the idea of the filter and tried to pass a variable into the function filter_where($var) but apparently add_filter(‘post_where’,’filter_where(‘.$var.’)’); does not take this too well. The script would run in the loop while but would halt after getting an error call on hook name.

    To fix that, I had to make the variable global inside the function. It works but appears to be a clumsy solution.

    <?php
    function filter_where() {
    	global $this_post;
    	global $this_author;
    	$w = " AND ID <=".$this_post." AND post_author=".$this_author;
    	return $w;
    }
    
    //sample first loop
    if (have_posts()) : while (have_posts()) : the_post();
    $what_post=$post->ID;						$who_author=$author_id;
    
    //assume an elaborate sorting condition statement
    if($found_author==$who_author || $found_post==$what_post){
          $this_author=$found_author;
          $this_post=$found_post;
    }
    
    //sample second loop
    $myPosts = new WP_Query();									add_filter('posts_where', 'filter_where');
    $myPosts->query('posts_per_page=10');
    while ($myPosts->have_posts()): $myPosts->the_post();
    the_content(__("(...)"));
    endwhile;
    
    //closing the first loop
    endwhile;
    endif;
    ?>

    Would appreciate any thoughts on this. Thanks!

    Thread Starter ianfulgar

    (@ianfulgar)

    Oops, sorry for misalignment. Posting the sample code again:

    <?php
    function filter_where() {
    	global $this_post;
    	global $this_author;
    	$w = " AND ID <=".$this_post." AND post_author=".$this_author;
    	return $w;
    }
    
    //sample first loop
    if (have_posts()) : while (have_posts()) : the_post();
    $what_post=$post->ID;
    $who_author=$author_id;
    
    //assume an elaborate sorting condition statement
    if($found_author==$who_author || $found_post==$what_post){
          $this_author=$found_author;
          $this_post=$found_post;
    }
    
    //sample second loop
    $myPosts = new WP_Query();
    add_filter('posts_where', 'filter_where');
    $myPosts->query('posts_per_page=10');
    while ($myPosts->have_posts()): $myPosts->the_post();
    the_content(__("(...)"));
    endwhile;
    
    //closing the first loop
    endwhile;
    endif;
    ?>

    On a quick glance, pretty sure you would want wp_reset_query(); before the //closing the first loop

    Thread Starter ianfulgar

    (@ianfulgar)

    Yes, you’re right. Just added in

    wp_reset_query();

    before closing first loop and now it performs better by resetting the query. Thanks MichaelH for catching that.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Query start at a particular post_id’ is closed to new replies.