Pagination Problem When Using Two Queries

Hi,
I’m having a real problem with one of my Wordpress category pages. I use a 3-column grid system on this page and just recently I needed to add an advert to the page, so I thought I would do one query at the top with the first two posts, then add the advert to complete the row. Then I would use a second query underneath creating the posts using the traditional grid system with 3 in each row.

This is working well except for the pagination, which adds the two items form the first query everytime it paginates. You can see this in action at http://soundsandcolours.com/music/

Does anyone know what I can do to make the Pagination ignore the first query, and just keep running the second query instead. I’ve included all of my code below:

1st Query:

$query_string; $musiccat_posts = new WP_Query($query_string."&posts_per_page=2"); if ($musiccat_posts->have_posts()): $musiccatIDs = array(); while($musiccat_posts->have_posts()):$musiccat_posts->the_post(); $musiccatIDs[] = get_the_ID();

2nd Query:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; global $moremusiccat_query; $music_args = array( 'cat' => 1841, 'posts_per_page' => 15, 'post__not_in' => $musiccatIDs, 'paged' => $paged ); $moremusiccat_query = new WP_Query( $music_args ); while ($moremusiccat_query -> have_posts()) : $moremusiccat_query -> the_post(); ?>

Pagination Code:

$big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $moremusiccat_query->max_num_pages ) );

Any help would be massively appreciated!

Do you reset your query before you start your second one?

Hey,
Yeah I’ve tried using both wp_reset_postdata(); and wp_reset_query(); after the first query, right after the endwhile loop as shown below, but it doesn’t seem to make any difference.

endwhile; wp_reset_query();

Any other ideas?

Cheers,
Russ

What kind of format is the ad in? Is it a custom post type, an image, a widget?

Hey WebMachine,
What do you mean by the ad? The ad itself is using code directly from an OpenX ad server.

Any ideas about what’s happening with the loop?

Cheers,
Russ

I was just going to try to duplicate the situation when I get some time to see what could be done, so I was wondering how the ad was inserted.

Hey WebMachine,
Thanks for the help. In which case, here’s the full code for the page. The page is called loop-ad.php and is called in the category.php page. If you want I can also send the category.php code if that’s of any help. Instead of using the ad code you could always just use a placeholder image with dimensions of 300x250.

Let me know if this helps at all.

Cheers!

[code]<?php $style_classes = array('first','second','third'); $styles_count = count($style_classes); $style_index = 0; ?>
<?php global $query_string; $musiccat_posts = new WP_Query($query_string."&posts_per_page=2"); if ($musiccat_posts->have_posts()): $musiccatIDs = array(); while($musiccat_posts->have_posts()):$musiccat_posts->the_post(); $musiccatIDs[] = get_the_ID(); ?>
<div class=“grid_5 <?php $k = $style_index % $styles_count; echo "$style_classes[$k]"; $style_index++; ?>”>
<article id=“post-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php $category = get_the_category($post->ID); $category = $category[0]->cat_ID; ?>
<?php if ( has_post_thumbnail() || get_post_meta($post->ID, 'image', true) || z_taxonomy_image_url($category, 'thumbnail') ) : ?>

<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<?php the_post_thumbnail('thumbnail'); // Declare pixel size you need inside the array ?>
<?php elseif ( z_taxonomy_image_url($category, 'thumbnail') ) : ?>
<?php the_title(); ?>
<?php elseif (get_post_meta($post->ID, 'image', true)) : ?>
<img src=“<?php echo get_post_meta($post->ID, "image", $single = true); ?>” alt=“<?php the_title(); ?>” class=“wp-post-image” />
<?php endif; ?>


<?php endif; ?>

		<h3><?php if (in_category(179) || in_category(462)) : echo "<span class='review'>REVIEW</span> "; elseif (in_category(65)) : echo "<span class='review'>MIXTAPE</span> "; endif; ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>

        <span class="author"><?php _e( 'By', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>
		<span class="date"> - <?php the_time('d F, Y'); ?></span>
		
		<?php html5wp_excerpt('html5wp_index'); ?>
		
		</article>
    </div>

<?php
endwhile;
wp_reset_query();
?>

    <div class="grid_5 third">
        <div align="center" style="margin-bottom:10px;">

		<!--/*
		  *
		  * Revive Adserver Asynchronous JS Tag
		  * - Generated with Revive Adserver v3.2.1
		  *
		  */-->

		<ins data-revive-zoneid="8" data-revive-id="9d7f6252bc593adad4212f4965ed0d40"></ins>
		<script async src="//soundsandcolours.com/ads/www/delivery/asyncjs.php"></script>
        </div>
    </div>    	

<?php
	$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
	global $moremusiccat_query;
    $music_args = array(
        'cat' => 1841,
        'posts_per_page' => 18,
        'post__not_in' => $musiccatIDs,
        'paged' => $paged
    );     		
	$moremusiccat_query = new WP_Query( $music_args );
	$style_index = 0;
	while ($moremusiccat_query -> have_posts()) : $moremusiccat_query -> the_post(); ?>
    <div class="grid_5 <?php $k = $style_index % $styles_count; echo "$style_classes[$k]"; $style_index++; ?>"> 
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		<?php
		$category = get_the_category($post->ID);
        $category = $category[0]->cat_ID;
		?>        
        <?php if ( has_post_thumbnail() || get_post_meta($post->ID, 'image', true) || z_taxonomy_image_url($category, 'thumbnail') ) : ?>
        		<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
					<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
                            <?php the_post_thumbnail('thumbnail'); // Declare pixel size you need inside the array ?>
                    <?php elseif ( z_taxonomy_image_url($category, 'thumbnail') ) : ?>
                            <img src="<?php echo z_taxonomy_image_url($category, 'thumbnail'); ?>" alt="<?php the_title(); ?>" class="wp-post-image" />
                    <?php elseif (get_post_meta($post->ID, 'image', true)) : ?>
                    		<img src="<?php echo get_post_meta($post->ID, "image", $single = true); ?>" alt="<?php the_title(); ?>" class="wp-post-image" />
                    <?php endif; ?>
                </a>                
            <div style="clear:left;"></div>
        <?php endif; ?>

		<h3><?php if (in_category(179) || in_category(462)) : echo "<span class='review'>REVIEW</span> "; elseif (in_category(65)) : echo "<span class='review'>MIXTAPE</span> "; endif; ?><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>

        <span class="author"><?php _e( 'By', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>
		<span class="date"> - <?php the_time('d F, Y'); ?></span>
		
		<?php html5wp_excerpt('html5wp_index'); ?>
		
		</article>
    </div>

<?php endwhile; ?>
<?php else: ?>
<article>
	<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<?php endif; ?>[/code]

I’ll get at it later tonight (my New Year’s Eve fun!). Right now I’m dealing with a couple of projects with today as the deadline. I hope that isn’t too late.

You have the posts displayed three per row. What if you used a slightly different strategy: have only one query with the pagination activated. But use css to restrict the width of the first row so that it contains only two posts, and then use positioning to place the ad where the third spot in the first row is. The rest of the queried posts would continue in their rows of three. That way you won’t have deal with two separate queries. Just a thought.

Yeah sure, that is definitely an option. I was hoping to retain the old way of doing it though, as it would require a lot of work to change the way that the posts are displayed. And, I figured, there must be a logical explanation for why the pagination is continuing both of those queries. But, at the moment, it doesn’t really seem like there’s any possible answer.

Thanks for your help though! And let me know if you have any ideas for why this could be happening. I hope you met your deadlines alright!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.