OiO.lk Blog PHP Custom Post Type pagination not working correctly
PHP

Custom Post Type pagination not working correctly


Trying to add pagination to a custom post type query. I’ve used this same code before, but it keeps breaking on me – sending to 404 page. Not sure what’s going on here but would love some help if anyone can see my error(s).

One note: In my Settings -> Permalinks admin area, I have it set to custom structure

/blog/%postname%/

which I don’t think should matter because of the rewrite rule.

<section id="section-portfolio">
    <div class="container">
        <h1><?php echo get_the_title(); ?></h1>
        <div class="portfolio-wrapper flex">
            <?php
            // grab all Portfolio CPT items
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            $args = array(
                'post_type' => 'portfolio',
                'post_status' => 'publish',
                'posts_per_page' => 4,
                'order'     => 'DESC',
                'paged' => $paged,
            );
            $query = new WP_Query($args);
            if ($query->have_posts()) :
                while ($query->have_posts()) :
                    $query->the_post();
                    // loop through Portfolio ITems
                    // Title
                    if (!empty(get_field('project_name_alt'))) {
                        $projectTitle = get_field('project_name_alt');
                    } elseif (!empty(get_field('short_project_name'))) {
                        $projectTitle = get_field('short_project_name');
                    } else {
                        $projectTitle = get_the_title();
                    }
            ?>
                    <div class="project" style="background-image: url('<?php echo get_field('bg_img'); ?>');">
                        <a href="<?php echo get_the_permalink(); ?>" title="Learn more about the <?php echo get_the_title(); ?> project">
                            <h2 class="title"><?php echo $projectTitle; ?></h2>
                        </a>
                    </div>
            <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </div>
        <div class="portfolio-pagination">
            <?php echo get_template_part('template-parts/portfolio-pagination'); ?>
        </div>
    </div>
</section>

Portfolio Pagination Template:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data = new WP_Query(array(
    'post_type' => 'portfolio', // your post type name
    'posts_per_page' => 4, // post per page
    'paged' => $paged,
));

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

    // Your code
    endwhile;


    $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'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
?>
<?php else : ?>
    <h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_query(); ?>

CPT Registry

    // PORTFOLIO ITEM
        $labels = array(
            'name'                => _x( 'Portfolios', 'Post Type General Name', 'greathouse' ),
            'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'greathouse' ),
            'menu_name'           => __( 'Portfolios', 'greathouse' ),
            'parent_item_colon'   => __( 'Parent Portfolio', 'greathouse' ),
            'all_items'           => __( 'All Portfolios', 'greathouse' ),
            'view_item'           => __( 'View Portfolio', 'greathouse' ),
            'add_new_item'        => __( 'Add New Portfolio', 'greathouse' ),
            'add_new'             => __( 'Add New Portfolio', 'greathouse' ),
            'edit_item'           => __( 'Edit Portfolio', 'greathouse' ),
            'update_item'         => __( 'Update Portfolio', 'greathouse' ),
            'search_items'        => __( 'Search Portfolios', 'greathouse' ),
            'not_found'           => __( 'Not Found', 'greathouse' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'greathouse' ),
        );
     
     
        $args = array(
            'label'               => __( 'portfolio', 'greathouse' ),
            'description'         => __( 'Portfolios', 'greathouse' ),
            'labels'              => $labels,
            // Features this CPT supports in Post Editor
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes'),
            // You can associate this CPT with a taxonomy or custom taxonomy. 
            'hierarchical'        => false,
            'public'              => true,
            'publicly_queryable' => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'menu_icon'            => 'dashicons-images-alt',
            'can_export'          => true,
            'has_archive'         => false, // use a cutom page template to display the CPT posts to allow use of PAGE data
            'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'page',
        );
     
        register_post_type( 'portfolio', $args );



You need to sign in to view this answers

Exit mobile version