Support » Fixing WordPress » Pagination wp_query not working

  • My code custom page:

    
    <div class="row">
    <?php
    $paging = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
     
    $args = array(
        'post_type' => 'post',
        'post_status'=>'publish',
        'posts_per_page' => 2,
        'paged' => $paging
    );
     
    $display_post_home = new WP_Query($args);
    ?>
    <?php if ( $display_post_home->have_posts() ) : ?>
    <?php while ( $display_post_home->have_posts() ) : $display_post_home->the_post(); ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    	
    <div class="col-sm-6 col-xs-12 single-post">
    	<div class="white-bg">
    		<a href="<?php the_permalink(); ?>" class="image">
    				<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" class="img-responsive">
    				<div class="shadow-bg"></div>
    				<div class="date"><?php the_date('l, d.m.Y', '', ''); ?></div>
    
    				<?php $images = get_field('gallery_posts'); if( $images ): ?>
    				<div class="gallery">
    					<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 420.8 420.8" xml:space="preserve">
    					<g>
    						<path d="M406.8,96.4c-8.4-8.8-20-14-33.2-14h-66.4v-0.8c0-10-4-19.6-10.8-26c-6.8-6.8-16-10.8-26-10.8h-120
    							c-10.4,0-19.6,4-26.4,10.8c-6.8,6.8-10.8,16-10.8,26v0.8h-66c-13.2,0-24.8,5.2-33.2,14c-8.4,8.4-14,20.4-14,33.2v199.2
    							C0,342,5.2,353.6,14,362c8.4,8.4,20.4,14,33.2,14h326.4c13.2,0,24.8-5.2,33.2-14c8.4-8.4,14-20.4,14-33.2V129.6
    							C420.8,116.4,415.6,104.8,406.8,96.4z M400,328.8h-0.4c0,7.2-2.8,13.6-7.6,18.4s-11.2,7.6-18.4,7.6H47.2
    							c-7.2,0-13.6-2.8-18.4-7.6c-4.8-4.8-7.6-11.2-7.6-18.4V129.6c0-7.2,2.8-13.6,7.6-18.4s11.2-7.6,18.4-7.6h77.2
    							c6,0,10.8-4.8,10.8-10.8V81.2c0-4.4,1.6-8.4,4.4-11.2s6.8-4.4,11.2-4.4h119.6c4.4,0,8.4,1.6,11.2,4.4c2.8,2.8,4.4,6.8,4.4,11.2
    							v11.6c0,6,4.8,10.8,10.8,10.8H374c7.2,0,13.6,2.8,18.4,7.6s7.6,11.2,7.6,18.4V328.8z"/>
    						<path d="M210.4,130.8c-27.2,0-52,11.2-69.6,28.8c-18,18-28.8,42.4-28.8,69.6s11.2,52,28.8,69.6c18,18,42.4,28.8,69.6,28.8
    							s52-11.2,69.6-28.8c18-18,28.8-42.4,28.8-69.6s-11.2-52-28.8-69.6C262.4,142,237.6,130.8,210.4,130.8z M264.8,284
    							c-14,13.6-33.2,22.4-54.4,22.4S170,297.6,156,284c-14-14-22.4-33.2-22.4-54.4c0-21.2,8.8-40.4,22.4-54.4
    							c14-14,33.2-22.4,54.4-22.4s40.4,8.8,54.4,22.4c14,14,22.4,33.2,22.4,54.4C287.6,250.8,278.8,270,264.8,284z"/>
    						<circle cx="352.8" cy="150" r="19.6"/>
    					</g>
    					</svg>
    				</div>
    				<?php endif; ?>
    			</a>
    			<div class="wrapper-post">
    				<?php $categories = get_the_category(); $color_cat = get_field( 'colors', 'category_'.$categories[0]->term_id); ?>
    				<div class="cat" style="background:<?php echo $color_cat; ?>"><?php echo $categories[0]->name; ?></div>
    
    			<a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
    			<p class="text-post"><?php the_field('first-text'); ?></p>
    			</div>
    	</div>
    </div>
    <?php endwhile; ?>
    
    <?php                    
    $big = 999999999;
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $display_post_home->max_num_pages
    ) );
    ?>
    
    <?php endif; ?>
    </div>
    

    Pages in the URL bar change, posts not..

    • This topic was modified 2 years, 3 months ago by herWALDI.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not entirely sure why, but attempting to use “paged” query vars in custom queries never seems to work very well. Somehow the internal query and custom query parameters get commingled or something. Instead of trying to sort this out, my advice is to completely divorce your query and pagination from anything related to paged query vars.

    Specify a custom paging query var in paginate_link()’s “format” argument. For example 'format'=>'?mypage=%#%'. In preparing your WP_Query, get the query var from $_GET[‘mypage’]. Explicitly cast it as an integer to sanitize it. In WP_Query, calculate your own “offset” argument instead of using “paged”. We are warned that using offset breaks pagination. In this case this is exactly what we want, to divorce our query from WP pagination and handle it ourselves.

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination wp_query not working’ is closed to new replies.