0

Having some trouble with pagination and custom post types.

I have 3 custom post types active currently: 'webinar', 'resource', and 'faq'.

For each of these custom post types, I'm using the archive-{post-type}.php file to show a list of the given post types.

For the /webinars and /faqs pages, this works as intended.

However for /resources, I'm looking to display a list of ALL of the following post types with pagination: 'post', 'webinar', 'resource'.

To do this, I have the following custom query:

<?php
                            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                            $custom_query = new WP_Query(
                                array( 
                                    'post_type' => array('resource', 'post', 'webinar'), 
                                    'posts_per_page' => 4,
                                    'paged'=> $paged, 
                                    'order' => 'desc',
                                    'orderby' => 'date') );

                            // Pagination fixes
                            $temp_query = $wp_query;
                            $wp_query   = NULL;
                            $wp_query   = $custom_query;
                        ?>
                        <?php if ( $custom_query->have_posts() ) : ?>
                            <div class='posts-ctn'>
                                <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
                                    <?php get_template_part( 'template-parts/resource/content', 'excerpt' ); ?>
                                <?php endwhile; ?> 
                            </div>
                            <?php
                                // Reset postdata
                                wp_reset_postdata();
                                // if ($custom_query->max_num_pages > 1) {
                                //  the_posts_pagination( array(
                                //      'prev_text' => '❮' . '<span class="screen-reader-text">' . __( 'Previous page', 'corax' ) . '</span>',
                                //      'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'corax' ) . '</span>' . '❯',
                                //      'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'corax' ) . ' </span>',
                                //  ) );
                                // }
                                // Custom query loop pagination
                                previous_posts_link( 'Older Posts' );
                                next_posts_link( 'Newer Posts', $custom_query->max_num_pages );

                                // Reset main query object
                                $wp_query = NULL;
                                $wp_query = $temp_query;
                            ?>
                        <?php else: ?>
                            <?php get_template_part( 'template-parts/resource/library', 'none' ); ?>
                        <?php endif ?>

This query returns the correct results, however when I click through to /resources/page/2, I get a 404 "oops, that page can't be found" page.

I have the following in my functions.php to also allow custom queries to return custom post types:

/**
 * This function modifies the main WordPress query to include an array of 
 * custom post types instead of the default 'post' post type.
 *
 * @param object $query  The original query.
 * @return object $query The amended query.
 */
function include_custom_post_types( $query ) {
    $custom_post_type = get_query_var( 'post_type' );

    if ( is_archive() ) {
        if ( empty( $custom_post_type ) ) $query->set( 'post_type' , get_post_types() );
    }

    if ( is_search() ) {
        if ( empty( $custom_post_type ) ) {
            $query->set( 'post_type' , array(
                'post', 'webinar', 'resource'
                )
            );
        }
    }

    return $query;
}

add_filter( 'pre_get_posts' , 'include_custom_post_types' );

I feel like I'm missing something really basic here, but I'm at a loss for what it might be. Help much appreciated!

  • If there is only one page of results, and you enter in the address page/{number}, where {number} is greater than one, you will receive a 404 error. – nmr Apr 8 at 20:11
  • @nmr I've confirmed there are multiple pages of results via $custom_query->max_num_pages and get more than 1 – blunatic Apr 8 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.