phpwordpresspaginationcustom-post-type

WordPress Custom post type issue with 10 posts per page and pagination


Im trying to create a pagination with a 'answer' post_type, bbut for some reason it creates the same 2 pages, meaning You get enough posts to fit 2 pages (20 posts) but it shows same posts on both and in the same order, what am I doing wrong?

$args_rel = array(
    'posts_per_page' => 10,
    'order' => 'DESC',
    'orderby' => 'ID',
    'post_type' => 'answer'
);

$wpex_query = new wp_query( $args_rel );
foreach( $wpex_query->posts as $post ){
    setup_postdata( $post );
    $tags = get_the_tags();
    echo '<div class="answer-wrapper" category="' . get_field('answer_cat') . '"><a class="rel-section" href="' . get_permalink() . '">' . get_the_title() . '
    <div class="rel-wrapper"><img class="rel-img" src="'. get_field('post_image') .'" /><p class="ans-name">' . get_field('name') . '<span class="inn-g">&nbsp; answered</span></p><div class="tags">'; 
    foreach ( $tags as $tag ) {
        echo "<span rel='tag'>{$tag->name}</span>";
    }
    echo '</div></div></a></div>';
}
$total_pages = $wpex_query->max_num_pages;

if ($total_pages > 1){

    $current_page = max(1, get_query_var('paged'));
    echo '<div class="pagination">';
    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text'    => __('«'),
        'next_text'    => __('»'),
    ));
    echo '</div>';
}
wp_reset_postdata();

And I should mention it was working fine but something happend along the way, didnt find any reason for this.


Solution

  • For pagination you need to add 'paged' in your WP_Query arguments and there are other mistakes.

    Don't use a foreach loop, instead use while with have_posts() method as recommended in the WordPress WP_Query documentation.

    Try the following instead:

    $answers = new wp_query( array(
        'posts_per_page' => 10,
        'paged'          => get_query_var('paged'),
        'order'          => 'DESC',
        'orderby'        => 'ID',
        'post_type'      => 'answer'
    ) );
    
    if ( $answers->have_posts() ) {
        while ( $answers->have_posts() ) : $answers->the_post();
    
            echo '<div class="answer-wrapper" category="' . get_field('answer_cat') . '"><a class="rel-section" href="' . get_permalink() . '">' . get_the_title() . '
            <div class="rel-wrapper">
                <img class="rel-img" src="'. get_field('post_image') .'" />
                <p class="ans-name">' . get_field('name') . '<span class="inn-g">&nbsp; answered</span></p>';
    
            if ( $tags = get_the_tags() ) {
                echo '<div class="tags">';
    
                foreach ( $tags as $tag ) {
                    echo "<span rel='tag'>{$tag->name}</span>";
                }
                echo '</div>';
            }
            echo '</div></a></div>';
        endwhile;
    
        $total_pages = $answers->max_num_pages;
    
        if ($total_pages > 1){
    
            echo '<div class="pagination">' . paginate_links( array(
                'base'      => get_pagenum_link(1) . '%_%',
                'format'    => '/page/%#%',
                'current'   => max(1, get_query_var('paged')),
                'total'     => $total_pages,
                'prev_text' => __('«'),
                'next_text' => __('»'),
            )) . '</div>';
        }
    } else {
        esc_html_e( 'Sorry, no answers were found.' );
    }
    wp_reset_postdata();
    

    It should better work.