0
votes

So I'm currently working on a custom plugin that is relying on custom post types. I am then displaying these custom post types in an admin page but can't seem to get the pagination to display! I have no idea what I'm doing wrong.

This is the loop I am running:

<?php
        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

        $mypost = array( 'posts_per_page' =>'1','paged'=>$paged,'post_type' => 'usernotes', 'meta_query' => array(
                array(
                    'key' => 'userNotesID',
                    'value' => get_current_user_id( )
                )
    ));
        $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
    <section class="innerWrapper">
            <article id="<?php the_ID(); ?>" class="note" <?php post_class(); ?>>
            <header class="entry-header">
                        <!-- Display Title and Author Name -->
                        <span class="title"><?php the_title(); ?></span>
                        <span class="date"><?php the_time(get_option('date_format')); ?></span>  
                </header>

                <div class="noteInner">
                    <?php the_content(); ?>
                </div>

                <footer>
                    <a href="'#" class="delete button-red" id="<?php the_ID(); ?>">Hide Note</a>
                    <div class="clear"></div>
                </footer>
            </article>
</section>


    <?php endwhile; ?>

It displays 1 post as it should and if I increase the posts_per_page to 2 then it as it should shows 2 posts. The problem is I can't get any pagination links to work. If I have it set to 1 and then navigation to ?paged=2, nothing changes, it just shows the first post only! Any idea why or what I can try?

EDIT:

Should probably add this is on an admin page, displaying custom post types.

1

1 Answers

0
votes

What about using a 'current' attribute in your query? Such as

'current' => max( 1, get_query_var('paged') )

I forgot where did I find this function but it works without any problem in my several projects. It makes things a lot easier to read and edit too.

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination($wp_query) {

        $big = 999999999; // need an unlikely integer

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'prev_text'    => __('<'),
            'next_text'    => __('>'),
            'type'         => 'plain',
        ) );
    }
endif;    

I hope this helps.