• I needed to create a calendar section on a WP site. “post_status=future” doesn’t work properly (only a logged in user can see the future posts) so I found a plugin that changes the post status to “published” and used the following code to skip the old posts:

    <?php query_posts('cat=5&showposts=10&order_by=date&order=ASC'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if(strtotime($post->post_date) < time())
    continue;
    ?>

    I thought this worked great, but I came across a problem. If I have 10 or more posts older than today’s date, it’ll query those 10 posts, then skip them and not display anything.

    Is there any way to either query posts after a certain date, or skip posts without adding to the counter?

Viewing 1 replies (of 1 total)
  • I had this same problem. I needed to add an offset to pass the old posts but wanted it to be automatic, of course. So I added a query to count the number of old posts:

    <?php
    $oldposts = $wpdb->get_var("SELECT COUNT(*)
    FROM $wpdb->posts wposts, $wpdb->term_relationships wterms
    WHERE wposts.ID = wterms.object_id
    AND wterms.term_taxonomy_id = '3'
    AND wposts.post_date < NOW()");
    ?>

    The term_taxonomy_id for my particular category was 3, but you would change it to 5 or whatever the value is.

    So, then you would change your query to:

    <?php query_posts('cat=5&showposts=10&offset=$oldposts&order_by=date&order=ASC'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if(strtotime($post->post_date) < time())
    continue;
    ?>

    Hope this helps.

Viewing 1 replies (of 1 total)
  • The topic ‘Problem skipping posts in a Loop’ is closed to new replies.