Pagination with WP_Query

I have done so much research this morning and I cannot find a solution to what is potentially a big problem. I’m trying to use wordpress pagination with a custom query and can’t seem to get it working!

Here is my code, Pagination just doesn’t show at all.

<?php //Template Name: Acapellas ?>
<?php get_header(); ?>
<div id="main-content">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <?php get_sidebar('primary-left'); ?>
            </div>
            <div class="col-md-9">
                <?php get_template_part( 'includes/ads', 'top' ); ?>
                <div class="post-content">
                    <h1>Free Acapellas</h1>

                    <?php if (pmpro_hasMembershipLevel(array(1,2), $user_id)) { ?>
                    <?php echo do_shortcode('[collections]'); ?>
                    <?php } ?>

                    <?php 
                    $args = array (
                            'post_type'              => array( 'acapella' ),
                            'pagination'             => true,
                            'posts_per_page'         => '12',
                            'ignore_sticky_posts'    => true,
                        );

                        // The Query
                        $query = new WP_Query( $args );

                    ?>
                    <?php if ( $query->have_posts() ) : ?>
                    <ul class="acapellas row">

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

                        <?php get_template_part( 'includes/list', 'acapella' ); ?>

                        <?php endwhile; ?>
                    </ul>

                    <?php // Previous/next page navigation.
                        the_posts_pagination( array(
                            'prev_text'          => __( 'Previous page', '' ),
                            'next_text'          => __( 'Next page', '' ),
                            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', '' ) . ' </span>',
                        ) );

                        // If no content, include the "No posts found" template.
                        else :
                            get_template_part( 'content', 'none' );

                        endif;
                        ?>

            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>

Related posts

2 comments

  1. Just download and install wp-pagenavi plugin and then use:

    if(method_exists('wp_pagenavi')){
        wp_pagenavi(array('query' => $query));
    }
    

    Pass your query object in wp_pagenavi method argument.

  2. i guess, you are seeking a numbered pagination for custom query, than try this article

    Kvcodes.

    here is the code.

    function kvcodes_pagination_fn($pages = '', $range = 2){  
     $showitems = ($range * 2)+1;     // This is the items range, that we can pass it as parameter depending on your necessary. 
    
     global $paged;  // Global variable to catch the page counts
     if(empty($paged)) $paged = 1;
    
     if($pages == '') {   // paged is not defined than its first page. just assign it first page.    
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
            $pages = 1;
     }   
    
     if(1 != $pages) { //For other pages, make the pagination work on other page queries     
         echo "<div class='kvc_pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";
    
         for ($i=1; $i <= $pages; $i++)    {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))   
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
         }
    
         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>n";
     }
     }
    

    place the function on your current theme, functions.php

    and use it on loop.php or index.php

    kvcodes_pagination_fn();
    

    and for the WP_Query example

    $custom_query = new WP_Query("post_type=receipes&author=kvcodes"); 
     while ($custom_query->have_posts()) : $custom_query->the_post(); 
    //  Show loop content...
    endwhile; 
    
     kvcodes_pagination_fn($custom_query->max_num_pages);
    

    that’s it.

Comments are closed.