• Resolved cosimo

    (@cosimoamicagmailcom)


    HI. I’m trying to do multiple queries to differents category in home page. I’m using this

    $args2 = array( 'numberposts' => 4, 'category' => '5', 'order'=> 'ASC', 'orderby' => 'title' );
    $postslist2 = get_posts( $args2 );
    foreach ($postslist2 as $post2) :  setup_postdata($post2);
    
    endforeach;

    Then

    $args3 = array( 'numberposts' => 4, 'category' => '6', 'order'=> 'ASC', 'orderby' => 'title' );
    $postslist3 = get_posts( $args3 );
    foreach ($postslist3 as $post3) :  setup_postdata($post3);
    
    endforeach;

    In practice, I make a loop for any category. The problem is that he show wrong posts. For example, he show a post of category 6 in the loop where i’ve indicated the category 4.

    Thanks for anyone who help me and excuse me for my bad english.

Viewing 5 replies - 1 through 5 (of 5 total)
  • It sounds like you need to reset the query after each loop. Try adding this after your <?php endforeach; ?>.

    <?php wp_reset_query();?>

    WP Reset Query Codex Reference

    Thread Starter cosimo

    (@cosimoamicagmailcom)

    Yes it’s the first thing i do, but not work.

    Thread Starter cosimo

    (@cosimoamicagmailcom)

    No one can help me? This is my loop

    <div class="travel_box_container">
           	<h3>Viaggi e crociere in primo piano</h3>
            	<?php
    			$args = array( 'numberposts' => 6, 'category' => '7', 'order'=> 'ASC', 'orderby' => 'title' );
    			$postslist = get_posts( $args );
    			foreach ($postslist as $post) :  setup_postdata($post);
    			?>
            	<div class="travel_box">
            		<a href="<?php the_permalink() ?>" title="Vedi <?php the_title_attribute(); ?>">
                    <? the_post_thumbnail() ?></a>
            		<h4><?php the_title(); ?></h4>
                	<?php the_excerpt(); ?>
                	<div class="details"><p><a href="<?php the_permalink() ?>" title="Vedi e prenota <?php the_title_attribute(); ?>">Vedi e prenota</a></p></div>
            		<div class="travel_box_angle">
                		<p>A partire da <span><?php echo get_post_meta($post->ID, 'prezzo_base', true) ?></span></p>
                	</div><!--chiusura di un angolo prezzo -->
            	</div><!-- chiusura di un travel box -->
                <?php endforeach; ?>
                <?php wp_reset_query();?>
                <div style="clear:both;"></div>
        </div><!-- chiusura di travel box container con i viaggi in primo piano-->
        <div style="clear:both;"></div>
        	<div class="bottom_wrapper">
            	<h3>Viaggi e crociere in offerta</h3>
            	<div class="bottom_wrapper_scroller">
                	<?php
    				$args2 = array( 'numberposts' => 4, 'category' => '5', 'order'=> 'ASC', 'orderby' => 'title' );
    				$postslist2 = get_posts( $args2 );
    				foreach ($postslist2 as $post2) :  setup_postdata($post2);
    				?>
            		<div class="bottom_box">
                		<a href="<?php the_permalink() ?>" title="Vedi <?php the_title_attribute(); ?>">
                        <? the_post_thumbnail(); ?></a>
                    	<h4><?php the_title(); ?></h4>
                    	<?php the_excerpt(); ?>
                    	<div class="details"><p><a href="<?php the_permalink(); ?>" title="Vedi e prenota <?php the_title_attribute(); ?>">Vedi e prenota</a></p></div>
                    	<div class="travel_box_angle">
                			<p>A partire da <span><?php echo get_post_meta($post->ID, 'prezzo_base', true) ?></span></p>
                		</div><!--chiusura di un angolo prezzo -->
                	</div><!-- chiusura di un bottom box -->
                    <?php endforeach; ?>
                    <?php wp_reset_query();?>
                    <div class="bottom_box_clear"></div>
            	</div><!-- chiusura di bottom wrapper scroller -->
             </div><!-- chiusura del bottom wrapper con i viaggi in offerta-->
             <div class="bottom_wrapper">
            	<h3>Viaggi e crociere <span class="red_title">last minute</span></h3>
                	<div class="bottom_wrapper_scroller">
             			<?php
    					$args3 = array( 'numberposts' => 4, 'category' => '6', 'order'=> 'ASC', 'orderby' => 'title' );
    					$postslist3 = get_posts( $args3 );
    					foreach ($postslist3 as $post3) :  setup_postdata($post3);
    					?>
                    	<div class="bottom_box">
                			<a href="<?php the_permalink() ?>" title="Vedi <?php the_title_attribute(); ?>">
                        	<? the_post_thumbnail() ?></a>
                    		<h4><?php the_title(); ?></h4>
                    		<?php the_excerpt(); ?>
                    		<div class="details"><p><a href="<?php the_permalink(); ?>" title="Vedi e prenota <?php the_title_attribute(); ?>">Vedi e prenota</a></p></div>
                    		<div class="travel_box_angle">
                				<p>A partire da <span><?php echo get_post_meta($post->ID, 'prezzo_base', true) ?></span></p>
                			</div><!--chiusura di un angolo prezzo -->
                		</div><!-- chiusura di un bottom box -->
                        <?php endforeach; ?>
                        <?php wp_reset_query();?>
                    	<div class="bottom_box_clear"></div>
            		</div><!-- chiusura di bottom wrapper scroller -->
            </div><!-- chiusura del secondo bottom wrapper con i last minute-->

    Seems there’s nothing wrong, but it dont’t work.

    This is what im using for multiple loops and its working just fine. It uses wp_reset_postdata(); to reset the loop. Im using this to display posts from six different categories. The categories are defined by name and number of posts are set as well. Hope this helps.

    http://pastebin.com/FD2vhgJJ

    Thread Starter cosimo

    (@cosimoamicagmailcom)

    HI, i use your code and now it works fine, thank you.
    But I can not understand why the other code does not work, it seems to be correct.
    I wish I could understand where the error is.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Wrong multiple queries using get_posts’ is closed to new replies.