Pagination when using wp_query?

<!-- query -->
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query = new WP_Query( array(
        'category_name' => 'investor-news',
        'posts_per_page' => 2,
        'paged' => $paged
    ) );

<?php if ( $query->have_posts() ) : ?>

<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" title="Read"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <?php echo get_the_date(); ?>

<?php endwhile; ?>
<!-- end loop -->

<!-- WHAT GOES HERE?????? -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

I've tried everything to achieve pagination on this static page using the wp_query function but without any luck. There's a comment in this script called WHAT GOES HERE?????... so what goes here?

This is on a static page that is not the front page or the posts page.


This code is for Custom Query Pagination. You can follow the steps to create your own pagination in WordPress.

* Template Name: Custom Page
get_header(); ?>

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 4,
  'paged' => $paged
$custom_query = new WP_Query( $args );
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

   while($custom_query->have_posts()) :
           <h3><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></h3>
        <div><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a></div>
        <p><?php echo the_content(); ?></p>
          </div> <!-- end blog posts -->
       <?php endwhile; ?>
      <?php if (function_exists("pagination")) {
      } ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
        <?php get_footer();

Reference :

Replace <!-- WHAT GOES HERE?????? --> with the pagination code below:

<div class="pagination">
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );

WordPress comes with a handy function called paginate_links() which does the heavy lifting. In the example above, the custom WP_Query object $query is used instead of the global $wp_query object.

You can simply use a built-in WP function


It does the job very well with custom WP_Query

Similar questions

Using one WP_Query object within the loop of another WP_Query object
I'm creating two different WP_Query objects, one for each of two different post types I'm using. In creating my output, I'd like to loop through one of the objects, and at the same time loop through the other object as well, so I can output pieces of them together. Here's an example of what I'd like to accomplish: However, that doesn't work. Taking...
Custom page pagination based on Kriesi pagination
I am stuck on this problem: We have 2 custom post types on our website, both created the same way, only the name of the post type differs. In addition we have 2 custom page templates which show an archive of these custom post types. Both use the same logic / look and feel only the custom post type posts shown differ. Our problem is that we use cust...
Search pagination link redirecting to a link that doesn't work with pagination
I have my search set up with a page called 'search' and a template called 'searchpage.php'. When I visit '' the URL changes to '' To solve this and make it work I used: However, my pagination links im creating use a URL structure of: WP changes this URL to: Which still shows a search page but the pag...
Pagination links missing for first link (1) and previous button? How to get pagination links to work?
I'm trying to add pagination to my WordPress query using pagination_links() however I am getting no links returned for my page 1 links and previous link? Other links are adding the paged query string to the URL which then redirect you to /page/3/ for example. Unsure whether I need to update the query below to add these URL's instead of the query st...
Custom loop pagination issue ( pagination not showing up )
I have a pagination issue in my custom loop, here is the loop. And my question is: Why pagination link not even showing up in this case?
Wordpress pagination loop show pagination html if is true
I'm using a pagination query in my loop. But I need it to output HTML only if the pagination is required. Because on some pages, the pagination markup will not be necessary because the page will only have a few posts. So currently in my loop, if the archive page shows 8 posts, my markup <div class="archive-navigation"> and </div> is sti...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.