WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with multiple wp_query loops (6 posts)

  1. Tuppaloprobe
    Member
    Posted 4 years ago #

    I have a page where I generate a table sorted based on country. I did this by using customfields and one wp_query pr country. I have a navigation sidebar on the left side. When the sidebar i active the table is generated with a lot of post that should not be there. The first country works fine and that is the only country for now that has any entries. But the other countries gets entries that does not have the same meta_key. I suspect that it gets all posts with the same meta_value regardles of the key. But when i remove the sidebar from the templatepage all the table queries works perfect.

    I have tried to add wp_reset_query(); but it did not make any difference.

    I have another question to. It does not seem to make any difference if i add 'cat' parameters to the query. The search seems to ignore the cat parameter regardless of what i put there.

    Here is the wp_query args. All look the same just a different country.
    The cat here does not make any difference if I add it or not.

    $args1 = array(
    	'cat'  => '50',
    	'showposts'  => 1000,
            'orderby'  =>  title,
            'meta_key'  => treningsland,
            'meta_value' => $text02, /*Sverige*/
            'order'  =>  asc,
    );

    Here is the code of one of the country entries. All look the same.

    <?php
    $my_posts = new WP_Query();
    $my_posts->query($args1);
    ?>
    
    <?php
    if ($my_posts->have_posts()) {
    ?>
    
    <div class="alignleft">
    
    <h2 class="header_course_table"><?php echo $text02 ?></h2>
    
    <table class="course_table">
    <tr>
    <th></th>
    <?php if (get_post_meta($post->ID, 'kursdatum', true)) { ?>
    <th><?php echo $text13; ?></th>
    <th><?php echo $text10; ?></th>
    <th><?php echo $text11; ?></th>
    <?php }
    else { ?>
    <th><?php echo $text09; ?></th>
    <th><?php echo $text10; ?></th>
    <th><?php echo $text11; ?></th>
    <th><?php echo $text12; ?></th>
    </tr>
    <?php } ?>
    
    <?php
    while ($my_posts->have_posts()) : $my_posts->the_post(); ?>
    
    <tr class="<?=($c++%2==1) ? 'odd' : 'even' ?>">
    <td>
    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"
    rel="bookmark" title="<?php echo $text06; ?><?php
    the_title_attribute(); ?>"><?php echo $text05 ?></a></h3>
    </td>
    
    <?php if (get_post_meta($post->ID, 'kursdatum', true)) { ?>
    <td>
    <?php echo get_post_meta($post->ID, 'kursdatum', true); ?>
    </td>
    <td>
    <?php echo get_post_meta($post->ID, 'Tid', true); ?>
    </td>
    <td>
    <?php echo get_post_meta($post->ID, 'kursort', true); ?>
    
    <?php }
    
    else { ?>
    <td>
    <?php echo get_post_meta($post->ID, 'Veckodag', true); ?>
    </td>
    <td>
    <?php echo get_post_meta($post->ID, 'Tid', true); ?>
    </td>
    <td>
    <?php echo get_post_meta($post->ID, 'kursort', true); ?>
    </td>
    <td>
    <?php echo get_post_meta($post->ID, 'kurstyp', true); ?>
    </td>
    <?php } ?>
    
    <?php endwhile;
    wp_reset_query(); ?>
    
    </table>
    </div>
    
    <?php } ?>
  2. Tuppaloprobe
    Member
    Posted 4 years ago #

    It seems that the query is forced to be in a certain way. I have tried different query parameters, but I get the same result every time when the sidebar is active.

  3. keesiemeijer
    moderator
    Posted 4 years ago #

    did you try this kind of loop

    <?php $temp_query = $wp_query; ?>
    <!-- Do stuff... -->
    
    <?php query_posts('category_name=special_cat&posts_per_page=10'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php endwhile; ?>
    
    // now back to our regularly scheduled programming
    <?php $wp_query = $temp_query; ?>

    I once tried the following and it worked. I don't now where I found this solution and if its standard markup but you could give it a try

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query($args1);

    before the loop. And after the loop you put this:
    <?php $wp_query = null; $wp_query = $temp; ?>

  4. keesiemeijer
    moderator
    Posted 4 years ago #

    You probably have another loop in your sidebar and its probably called before the actual page loop. So you have multiple loops that interfere with eachother. If you queried the loop in the sidebar the same way as on the page, you can change the variable with a unique name
    $my_posts = new WP_Query(); to $my_sidebar_posts = new WP_Query();
    If you have a loop in the sidebar you do the wp_reset_query there.

  5. Tuppaloprobe
    Member
    Posted 4 years ago #

    I figured out why.

    I copied the same loop and the same args and used them both for the sidebar and for the loops above and thus there where more than 1 array with different parameters and same name. The array in my post did not write over them and the first args where the ones used in both the side bar and this page.

    Thanks a lot for the help anyway :)

  6. keesiemeijer
    moderator
    Posted 4 years ago #

    I'm glad you found the solution to this problem

Topic Closed

This topic has been closed to new replies.

About this Topic