0

I am trying to create a page with three wp_queries. The first query pulls the first 2 articles of a given category, the second pulls the next three from the same category (offset 2 in $args) and the next pulls the remainder using (offset 5) with pagination. The first two queries are performing as expected but the last one is giving me an option to go to page two and is showing identical results from page 1.

The reason I am doing this is because I want the first two posts to format one way, the second query with the next three posts to format another way and the third (with pagination) to format another way.

I know there is a way I could probably do this with less code I just don't know how. =( Any help would be greatly appreciated.

Here is my code:

     <style>
     .category-master-3-tiles {
             height: 350px;
        background-size: cover;
        background-position: center;
     }

     .byline-category  {
         display:none;
     }

     </style>
    <?php get_header(); 
    $categoryName = get_field('category_name'); 
    $args = array(
          'post_type' => 'post',
          'category_name' => $categoryName,
          'posts_per_page' => 2,
          'paged' => $paged
        );
    $args2 = array(
          'post_type' => 'post',
          'category_name' => $categoryName,
          'posts_per_page' => 3,
          'offset' => 2,
          'paged' => $paged
        );

    $args3 = array(
          'post_type' => 'post',
          'category_name' => $categoryName,
          'posts_per_page' => 5,
          'offset' => 5,
          'paged' => $paged
        );
    ?>



    <!-----------------------------------------------FIRST ROW OF POSTS-->


    <div class="row large-collapse medium-uncollapse"  style="max-width:100%; width:100%;" >
    <?php $custom_query = new WP_Query($args); 
    while($custom_query->have_posts()) : $custom_query->the_post(); $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
        <div class="large-6 medium-6 small-12 columns category-master-3-tiles" style="background-image:url(<?php echo $url;?>);">
        <a href="<?php the_permalink(); ?>" style="display:block;"></a> <div style="position:absolute; top:10px; left:10px; color:#fff;"><?php the_title(); ?></div>
        </div>
    <?php endwhile; ?>
    </div>
    <?php wp_reset_postdata(); wp_reset_query();// reset the query ?>


    <!-----------------------------------------------SECOND ROW OF POSTS-->


    <div class="row"  style="max-width:100%; width:100%;" >
    <?php $custom_query2 = new WP_Query($args2); 
    while($custom_query2->have_posts()) : $custom_query2->the_post(); $url2 = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
        <div class="large-4 medium-4 small-12 columns category-master-3-tiles" style="background-image:url(<?php echo $url2;?>);">
        <a href="<?php the_permalink(); ?>" style="display:block;"></a> 
        </div>
    <?php endwhile; ?>
    </div>
    <?php wp_reset_postdata(); wp_reset_query();// reset the query ?>

    <!-----------------------------------------------BLOG ARCHIVE STYLE-->
    <?php 
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;



      $custom_query3 = new WP_Query( $args3 );
    ?>
      <?php if ( $custom_query3->have_posts() ) : ?>
      <div class="row">
      <div class="large-8 medium-8 columns">
      <!-- the loop -->
      <?php while ( $custom_query3->have_posts() ) : $custom_query3->the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article">                  
            <header class="article-header">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </h2>
                <?php get_template_part( 'parts/content', 'byline' ); ?>
            </header> <!-- end article header -->

            <section class="entry-content" itemprop="articleBody">

                <?php the_excerpt('<button class="tiny">Read more...</button>'); ?>
            </section> <!-- end article section -->

            <footer class="article-footer">
                <?php /*?><p class="tags"><?php the_tags('<span class="tags-title">' . __('Tags:', 'jointstheme') . '</span> ', ', ', ''); ?></p><?php */?>
            </footer> <!-- end article footer -->                                           
        </article> <!-- end article -->
      <?php endwhile; ?>
      </div>
      <div class="large-4 medium-4 columns">
      <?php include('sidebar-category-pages.php'); ?>
      </div>
      <!-- end of the loop --> 
    </div>
    <!-- pagination here -->
    <?php
          if (function_exists(custom_pagination)) {
            custom_pagination($custom_query3->max_num_pages,"",$paged);
          }
        ?>
    <?php wp_reset_postdata(); wp_reset_query();?>
    <?php else:  ?>
    <p>
      <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
    </p>
    <?php endif; ?>


    <?php get_footer(); ?>
0

At first glance, the $paged that you provide to the three argument arrays is undefined; you only define it before the third query. Either put the $args3 definition AFTER you define $paged (mind you, in this case the first two queries will still run with $paged undefined, most likely generating PHP notices in the server log), or put $paged definition BEFORE all three argument arrays.

See if that changes anything.

| improve this answer | |
  • Thanks you very much for your response. So I've tried the following: I've removed both of the 'paged' => $paged from $args1 and $args2 It seems to work but I've tested it by having it pull posts from a category with only 9 posts. Being that the first two queries pull 5 posts and the third query (with pagination) is set to pull 5 more - while only having 4 posts left to pull in this category, I am still getting a link to page 2 and it's duplicating the 4 posts shown on page 1. If I had hair I'd be pulling it out at this point. Thanks in advance for any help on this. – C. Ruben Dec 14 '15 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.