WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] posts_per_page is giving unreliable results (7 posts)

  1. surfsup74
    Member
    Posted 2 years ago #

    I keep getting a different number of post_per_page from this loop.
    Sometimes 1 and sometimes 2. If I change it to post_per_page=3 I sometimes get 2 and sometimes 3 - you get the idea? I don't get it, any ideas anyone?
    http://pastebin.com/nTRcEmvU

    I have one other loop running before the unreliable one which is below:
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php $do_not_duplicate = $post->ID; ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php endif; ?>

    and another one after it (below):
    <?php $sidebar = new WP_Query('post_type=sidebar'); ?>
    <?php while ($sidebar->have_posts()) : $sidebar->the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    I don't see why the two other loops would be messing with the unreliable one... any ideas?

    Thanks

    Dan

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    line 4 <?php if( $post->ID == $do_not_duplicate ) continue;?> is simply jumping over apost (i.e. not showing it) if there is a duplicate post in the loop;

    for an alternative method, carefuly study http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    adapted to your case:

    change line 1 of the pastebin:

    <?php $my_query = new WP_Query(array('post_type' => 'page', 'orderby' => 'rand', 'posts_per_page' => 2, 'post__not_in' => array($do_not_duplicate))); ?>

    and remove line 4

  3. surfsup74
    Member
    Posted 2 years ago #

    Thanks for the reply - however I am still having exactly the same problem. Most of the time I get 2 post (as I want) but sometimes I just get 1. So the loop appears to still be jumping the $do_not_duplicate post and displaying nothing in it' place. I've read through the page you linked to (that was actually what I based my original loop on) but no joy... Could one of the other loops on the page be affecting it?

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    however I am still having exactly the same problem

    what have you changed?
    what is the new code of your loop?

  5. surfsup74
    Member
    Posted 2 years ago #

    Hi.

    I did as you suggested (changed line 1 and removed line 4). I have pasted the complete html into pastebin if you have a moment to look over it - http://pastebin.com/nhBYiU8U. The loop that's not working quite right starts line 44.

    Thanks

    Dan

  6. alchymyth
    Forum Moderator
    Posted 2 years ago #

    i checked the code in a test blog, and there were always two random pages (which is obviously no absolute proof that the code should be working under all circumstances);

    could your problem be caused by the '$selected_values' section of your code?

  7. surfsup74
    Member
    Posted 2 years ago #

    Thanks again. What could I change the $selected_values to? I found this http://wordpress.org/support/topic/plugin-simple-fields-fields-not-being-called-after-custom-loop-query?replies=3 on the forum and thought it might be useful but i still get either 1 or 2 results not always 2! Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic