• PDIMG

    (@stefanristicdev)


    Hi and thanks for taking a look, something that should be simple is bugging me all day, so I decided to post it here and see if we can fix this.

    I have 3 queries on my homepage, first one displays slider, second one displays carousel, and third one displays the homepage content.

    The problem- no matter what I use(WP_Query, get_posts or query_posts), the carousel shows duplicate results.

    First query(works fine):

    <ul class="slides">
    		<?php
    		$args = array (
    		'post_type' => 'slide',
    		);
    		$the_query = new WP_Query( $args );
    		if ( have_posts() ) :  while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<?php
    		$thumb_id = get_post_thumbnail_id();
    		$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
    		?>
    		<li><?php echo '<a href="' . get_field( "slide_link" ) . '"><img src="' . $thumb_url[0] . '" /></a>' ?></li>
    		<?php endwhile; ?>
    		<?php endif; ?>
    		<?php wp_reset_query(); ?>
    		<?php wp_reset_postdata(); ?>
    </ul>

    Second query(showing duplicate results):

    <ul class="amazingcarousel-list">
    		<?php
    		$posts = get_posts(array(
    			'post_type' => array('post', 'page', 'featured_rides'),
    			'meta_query' => array(array(
                            'key' => 'display_this_entry_inside_homepage_carousel',
                            'value' => '1',
                            'compare' => '=='
                            )
                            )
                            ));
    
    	if( $posts ): ?>
    
    	<?php foreach( $posts as $post ):
    	setup_postdata( $post )
            ?>
    	<li class="amazingcarousel-item">
            <div class="amazingcarousel-item-container">
            <div class="amazingcarousel-image"><a href="<?php echo get_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a></div>
             <div class="amazingcarousel-title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></div></div>
    	</li>
    	<?php endforeach; ?>
    	<?php endif; ?>
    	<?php wp_reset_postdata(); ?>
    	<?php wp_reset_query(); ?>
            </ul>

    Note re second query: If I use a single post_type value, for example ‘post’, it works fine(no duplicates).

    Third query is not really a query, I’m just using the “the_content()” to display page content, and it works fine as well.

The topic ‘Query showing duplicate results’ is closed to new replies.