Forums

query_post start att particular post id (4 posts)

  1. michaelfredman
    Member
    Posted 1 year ago #

    Hi!
    Does anyone know how to make query_post start at a particular post id?

    I have a site where I display a post in "single.php", but also want to display the posts following that post. I do that in a new query_post, but I can only make it show the post with an offset, and since I don't know the offset, I want to start at a particular post.

    Please help, going live pretty soon :S

    Thanks!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I don't think you can start at a particular ID because the ID's are not in any definite order. You should be able to get posts done on the same date or later by adapting the filter shown in the Codex here.

  3. michaelfredman
    Member
    Posted 1 year ago #

    Thanks vtxyzzy!
    How would you go on that query? I need the date variable from my post but then I suppose I need it in the filter WordPress describes below:

    function filter_where($where = '') {
      //posts for March 1 to March 15, 2009
      $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
      return $where;
    }

    I would then like to do something like

    function filter_where($where = '') {
      //posts for March 1 to March 15, 2009
      $where .= " AND post_date > '" . $myTime;
      return $where;
    }

    But how do I pass that variable to my filter?
    Thanks!

  4. vtxyzzy
    Member
    Posted 1 year ago #

    This is UNTESTED! In your single post loop at the top, save the date to a variable:

    global $myTime;
    if (have_posts()) :
       while (have_posts()) : the_post();
          $myTime = $post->post_date;
          // Rest of first loop
       endwhile;
    endif;

    Then in the filter, use something like this:

    function filter_where($where = '') {
       //posts for post_date greater than globally saved date
       global $myTime;
       if ($myTime) {
          $where .= " AND post_date > '" . $myTime . "'";
       }
      return $where;
    }

    If you have more than one post on a date, you may want to change the 'post_date > ' part to 'post_date >= ', and modify single.php to save the first post ID and exclude it in the second query.

Topic Closed

This topic has been closed to new replies.

About this Topic