Pagination For Custom Post Type In WordPress

Share Now

<? php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$data = new WP_Query(array(
    'post_type' => 'press', // your post type name
    'posts_per_page' => 10,
    'post_status' => 'publish',
    //'orderby' => 'menu_order',
    'meta_key' => 'press_publish_date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'ignore_sticky_posts' => true,
    'paged' => $paged,
)); ? >

<? php

if ($data - > have_posts()):
    while ($data - > have_posts()): $data - > the_post(); ? >

<? php get_template_part('template-part/press-block'); ? >

<? php endwhile; ? >

#Pagination Part

<div class = "news-list-pagination" >

<? php

$total_pages = $data - > max_num_pages;

if ($total_pages > 1) {

    $current_page = max(1, get_query_var('paged'));

    echo paginate_links(array(
        'base' => get_pagenum_link(1).
        '%_%',
        //'format' => '/page/%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'prev_text' => __('<i class="fas fa-chevron-left page-prev"></i>'),
        'next_text' => __('<i class="fas fa-chevron-right page-next"></i>'),
    ));
} ?>

<? php
else : ? >
    <h3 > < ? php _e('404 Error&#58; Not Found', ''); ? > < /h3> <? php endif; ? >
<? php wp_reset_postdata(); ? >

</div>
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert