• Resolved surfsup74

    (@surfsup74)


    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?
    https://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

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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 https://codex.www.ads-software.com/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

    Thread Starter surfsup74

    (@surfsup74)

    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?

    however I am still having exactly the same problem

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

    Thread Starter surfsup74

    (@surfsup74)

    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 – https://pastebin.com/nhBYiU8U. The loop that’s not working quite right starts line 44.

    Thanks

    Dan

    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?

    Thread Starter surfsup74

    (@surfsup74)

    Thanks again. What could I change the $selected_values to? I found this https://www.ads-software.com/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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘posts_per_page is giving unreliable results’ is closed to new replies.