1

I have a custom taxonomy with several quotes assigned to each term. Two terms (general and yoga) share the same pool of three quotes. On my front-page.php i randomly draw one quote from each term and display it as an introduction to each section. In the case of the terms general and yoga i've included a failsafe, the variable $do_not_duplicate, to prevent that in the second draw the same quote is picked again and that i have the same quote twice on a single page. The code of the first block is:

    <blockquote class="container" style="color:#287da2;">
    <?php
        $args = array(
            'post_type' => 'quotes',
            'thesections' => 'general',
            'posts_per_page' => 1,
            'orderby' => 'rand'
        );
        $the_query = new WP_Query( $args );
        if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); $do_not_duplicate = $post->ID; ?>
        <q><?php the_field( 'quote_text' ); ?></q><br><cite><?php the_field( 'quote_origin' ); ?></cite>
        <?php endwhile;
                wp_reset_postdata();
            else: ?>
            <p>DB error</p>
        <?php endif; ?>
    </blockquote>

The code of the second block is:

    <blockquote class="container" style="color:#1f8665;">
    <?php
        $args = array(
            'post_type' => 'quotes',
            'thesections' => 'yoga',
            'posts_per_page' => 1,
            'orderby' => 'rand'
        );
        $the_query = new WP_Query( $args );
        if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
        <q><?php the_field( 'quote_text' ); ?></q><br><cite><?php the_field( 'quote_origin' ); ?></cite>
        <?php endwhile;
                wp_reset_postdata();
            else: ?>
            <p>DB error</p>
        <?php endif; ?>
    </blockquote>

Problem is, it works most of the time, but after two or three page reloads, where the quotes are updated properly and no duplicates are shown, it happens that the second block remains blank with no output while the first block shows a quote. No php errors in the logs are shown, nor errors in wp debug mode - nothing. Is it due to the small number of quotes in the term (3 quotes) or is there an error in the code? I literally have no idea what the problem is in this case and what leads to those no shows in the second block every few reloads. :/ Best regards Ralf

1 Answer 1

2

The problem is how you're doing the exclusion. It's happening after you've grabbed your second quote. If it matches the first quote, it just silently fails to appear.

Try this for your 2nd block:

<blockquote class="container" style="color:#1f8665;">
<?php
    $args = array(
        'post_type' => 'quotes',
        'thesections' => 'yoga',
        'posts_per_page' => 1,
        'post__not_in' => array( $do_not_duplicate ),
        'orderby' => 'rand'
    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <q><?php the_field( 'quote_text' ); ?></q><br><cite><?php the_field( 'quote_origin' ); ?></cite>
    <?php endwhile;
            wp_reset_postdata();
        else: ?>
        <p>DB error</p>
    <?php endif; ?>
</blockquote>

The post__not_in parameter will prevent WP_Query from selecting your $do_not_duplicate post in the first place.

Reference

2
  • Thanks for explanation and clarification as well the suggestion to solve the issue. Works perfectly! Thanks again!
    – rkoller
    Jun 12, 2014 at 16:53
  • Glad I could help.
    – Pat J
    Jun 12, 2014 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.