Support » Plugin: YARPP - Yet Another Related Posts Plugin » Exclude from WP_Query Loop

  • Howdy!

    We’ve been using YARRP for a long time with the standard “Automatically display related content on” posts option. It works great, thanks!

    However we’re implementing a new bridge custom Post Type called Film which can reference one or more posts where the film has been reviewed. If I grab the related posts with a get_posts() call I can loop through them in a much more complicated manner than a WP_Query() call. Unfortunately YARRP has decided I’m showing each of these posts rather than creating my own mini-archive listing and is placing Related Posts below each summary.

    None of our other plugins like Social Sharing are displaying below these posts. I can’t spot any obvious setting to prevent YARRP from hooking into this loop. Since YARRP doesn’t do this on archive pages I’m baffled.

    How can I prevent this from happening or do I have to abandon the use of WP_Query() as long as we use YARRP?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support aslamdoctor

    (@aslamdoctor)

    Hello @clubside

    Can you show us what settings you have made under WordPress Admin for YARPP?
    Also what happens if you disable auto-populated YARPP posts from displaying below each post? Does it show Social Sharing buttons then?

    Thank You

    • This reply was modified 2 months, 3 weeks ago by aslamdoctor.
    Thread Starter clubside

    (@clubside)

    Howdy @aslamdoctor

    I have since switch to a non-loop to avoid the issue but it would be nice to see a fix in case anyone else has the problem.

    Here are our settings: https://outlawvern.com/posters/yarpp-settings.png

    I haven’t tried disabling the auto-population as the site is actively used. I could always enable it for Film or Icon Custom Post Types if you think that could help debug. Anyway the Easy Social Share Buttons which have a similar “select post types to automatically show” setting did not appear.

    I am in the process of developing a new theme. In case our settings are not a help I will re-implement the code that caused me to create this post and add a fresh reply here with a working link and the code that is causing it.

    Thanks so much for your help,
    Chris

    Thread Starter clubside

    (@clubside)

    Hey @aslamdoctor

    I have reverted the code on the live server and I do need to solve this because using the_content() does not work correctly in a get_posts() loop (the read more link reference the page you are on rather than the post in the foreach.

    Here is the code that causes Related Posts to appear, it’s nothing fancy and as I mentioned none of the other “automatically attach somewhere on posts” plugins render:

    <?php
    
    $query = new WP_Query( $args );
    		
    while ($query->have_posts()) {
    	$query->the_post();
    ?>
    	<div <?php post_class( 'vern-plainpost' ) ?> id="post-<?php the_ID(); ?>">
    
    		<div class="vern-plainheader" style="background: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'full' ) ); ?>') no-repeat top center; background-size: cover;">
    
    			<?php if(get_field('vern_is_bruce')) { ?>
    			<img class="bruce" src="/wp-content/uploads/2009/04/bruce.jpg" alt="" />
    			<?php } ?>
    
    			<?php
    			$terms = get_the_terms( get_the_ID(), 'review_series' );
    									 
    			if ( $terms && ! is_wp_error( $terms ) ) { ?>
    			<div class="vern-plainheaderseries">
    			<?php
    				foreach ( $terms as $term ) {
    					$image = get_field('review_series_icon', $term);
    			?>
    					<img src="<?php echo $image; ?>" /><br>
    			<?php
    				}
    			?>
    			</div>
    				
    			<?php } ?>
    			<div class="vern-plainheadertitle">
    				<a href="<?php echo get_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php echo esc_html( get_the_title() ); ?>"><?php echo get_the_title(); ?></a>
    			</div>
    			<div class="vern-plainheaderinfo">
    				<?php echo get_the_time( 'F jS, Y' ); ?> | 
    				<?php
    					$number = (int) get_comments_number();
    					if ( $number === 0)
    						echo 'No Comments';
    					elseif ( $number === 1)
    						echo '1 Comment';
    					else
    						echo $number . ' Comments';
    					// comments_popup_link('No Comments', '1 Comment', '% Comments');
    				?>
    				<?php edit_post_link('EDIT', ' | ', ''); ?>
    			</div>
    		</div>
    		
    		<div class="entry">
    			<?php the_content('(read the rest...)'); ?>
    		</div>
    
    	</div>
    <?php
    
    	}
    
    wp_reset_postdata();
    ?>

    Is the problem with the_content()?

    Really appreciate any help you can provide.
    Chris

    Plugin Support aslamdoctor

    (@aslamdoctor)

    Hi @clubside

    We have 2 questions:
    1. Have you use setup_postdata($post); in the get_posts() records loop?
    As it is very important for the helper methods like the_content() and the_title() to work properly.

    2. Also after the loop finishes, have you done wp_reset_postdata()?. Because it is very important to restore the global variable $post to its previous value.

    Here is a very basic example of get_posts() which will help you with this.

    <ul>
        <?php
        global $post;
     
        $myposts = get_posts( array(
            'posts_per_page' => 5,
        ) );
     
        if ( $myposts ) {
            foreach ( $myposts as $post ) : 
                setup_postdata( $post ); // add this in the very beginning of the loop ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
            endforeach;
            wp_reset_postdata(); // add this after closing the loop immediately.
        }
        ?>
    </ul>
    • This reply was modified 2 months, 1 week ago by aslamdoctor.
    • This reply was modified 2 months, 1 week ago by aslamdoctor.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.