Support » Fixing WordPress » Pagination wp_query not working

  • My code custom page:

    <div class="row">
    $paging = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $args = array(
        'post_type' => 'post',
        '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="" xmlns:xlink="" x="0px" y="0px" viewBox="0 0 420.8 420.8" xml:space="preserve">
    						<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
    							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
    						<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
    						<circle cx="352.8" cy="150" r="19.6"/>
    				<?php endif; ?>
    			<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>
    <?php endwhile; ?>
    $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; ?>

    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


    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.