Support » Developing with WordPress » Custom post type pagination problem

  • Resolved doubleedesign

    (@doubleedesign)


    I have a custom post type for frequently asked questions, called Question. Its archive is at /faq/.

    Page 1 and 2 of the archive currently show the same content. I did some digging and on page 1, is_paged() returns as false, and $wp_query->query_vars['paged'] returns as 0, whereas on page 2 they return as true and 2 respectively (as expected).

    I do have a custom archive template called archive-question.php, but this issue occurs even if using the default index.php.

    Other CPTs used on the site do not have this issue. Any suggestions as to what could be causing it would be a big help!

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator bcworkz

    (@bcworkz)

    The first archive page’s paged values being false and 0 is normal, albeit illogical. The usual reason people get redundant posts after page 1 is they are running a custom query that tries to use the main query’s paged values. But if your templates are not using custom queries, clearly the problem is elsewhere.

    It’s difficult to imagine the main query returning the same content whether paged is 0 or 2. Have a look at global $wp_query->request (the actual SQL query run) in each case just before the loop starts. Usually only the LIMIT clause is different. In your case even that is probably the same, but it would be telling if something is different.

    How or where did you determine the is_paged and paged values? Something appears to be altering the query vars or SQL between your investigation and the final SQL. You could try deactivating all plugins. If you still have trouble, there’s a conflict within your theme somewhere.

    Are you using a custom query to fetch the posts under FAQs?
    Do you mind posting the code of your templates? It is difficult to tell what is wrong without having something to look at

    • This reply was modified 1 year, 6 months ago by shariqkhan2012. Reason: Fixed typo

    Thanks so much for your replies!

    How or where did you determine the is_paged and paged values?

    Inside the template – commented out below.

    Are you using a custom query to fetch the posts under FAQs?

    No, I do have a custom search template for FAQ but this issue occurs even when just listing them using the default index.php.

    Do you mind posting the code of your templates?

    Below is index.php

    <?php get_header(); ?>
    
    <?php get_template_part('template-parts/top-banner'); ?>
    
    <section id="page" class="posts archive row align-center">
    
    	<div class="small-12 medium-10 xlarge-8 columns">
    
    		<main class="nested row align-justify">
    			<div class="small-12 columns">
    				<?php
    				if ( have_posts() ) {
    					/*
    					global $wp_query;
    					print_r($wp_query->query_vars['paged']);
    					if(is_paged()) {
    						echo "Yep, paged";
    					} else {
    						echo "Nope, not paged";
    					} */
    					// Start the Loop
    					while ( have_posts() ) : the_post();
    						get_template_part('excerpts/excerpt-list');
    					endwhile;
    				} else {
    					get_template_part('template-parts/nothing-found');
    				} // End have_posts() check.
    				?>
    			</div>
    		</main>
    
    	</div>
    
    	<?php // Display navigation to next/previous pages when applicable 
    	if ( function_exists( 'apso_pagination' ) ) { 
    		apso_pagination(); 
    	} else if ( is_paged() ) { ?>
    		<nav id="post-nav" class="row align-center align-justify">
    			<div class="post-previous columns"><?php next_posts_link( __( '&larr; Older posts', '' ) ); ?></div>
    			<div class="post-next columns"><?php previous_posts_link( __( 'Newer posts &rarr;', '' ) ); ?></div>
    		</nav>
    	<?php } ?>
    
    </section>
    
    <?php get_template_part('template-parts/footer-banner'); ?>
    
    <?php get_footer(); ?>

    The code above does not give away any hints.

    1. What is the setup for the front-page and the posts page in Dashboard->Settings->Reading?

    2. How have you ensured /faq/ is the archive page for the FAQs? Is ‘/faq’ the slug for the custom post type in question?

    3. Which template is being used to display the archive? Install a plugin like Show Current Template and see the actual template being used for the archives.
    Paste the code of that template here.

    4. Check the code where the custom post type is being registered. See what parameters are being passed to 'register_post_type'. The rewrite parameter can have effects on pagination.

    5. Lastly, try flushing the permalinks. Often that fixes pagination and 404 related problems.

    1. What is the setup for the front-page and the posts page in Dashboard->Settings->Reading?

    Homepage is a static page, Posts page is News (so shouldn’t be related to the FAQ)

    3. Which template is being used to display the archive? Install a plugin like Show Current Template and see the actual template being used for the archives.
    Paste the code of that template here.

    I have a code snippet to do the same thing and can confirm that the above code is index.php (I do have a separate archive-question.php that I have temporarily disabled as a process of elimination step…if I can get it working on the default template, then I can go from there)

    5. Lastly, try flushing the permalinks. Often that fixes pagination and 404 related problems.

    Yep tried that 🙁

    4. Check the code where the custom post type is being registered. See what parameters are being passed to ‘register_post_type’. The rewrite parameter can have effects on pagination.

    Yeah surely it has to be something here…it’s not the only CPT on the site, but it’s the only one with this issue. Here’s the CPT registration:

    <?php
    // Register Custom Post Type
    function doublee_question() {
    
    	$labels = array(
    		'name'                  => _x( 'Frequently Asked Questions', 'Post Type General Name', 'doublee' ),
    		'singular_name'         => _x( 'Question', 'Post Type Singular Name', 'doublee' ),
    		'menu_name'             => __( 'Questions', 'doublee' ),
    		'name_admin_bar'        => __( 'Question', 'doublee' ),
    		'archives'              => __( 'Question Archives', 'doublee' ),
    		'attributes'            => __( 'Question Attributes', 'doublee' ),
    		'parent_item_colon'     => __( 'Parent Question:', 'doublee' ),
    		'all_items'             => __( 'All Questions', 'doublee' ),
    		'add_new_item'          => __( 'Add New Question', 'doublee' ),
    		'add_new'               => __( 'Add New', 'doublee' ),
    		'new_item'              => __( 'New Question', 'doublee' ),
    		'edit_item'             => __( 'Edit Question', 'doublee' ),
    		'update_item'           => __( 'Update Question', 'doublee' ),
    		'view_item'             => __( 'View Question', 'doublee' ),
    		'view_items'            => __( 'View Questions', 'doublee' ),
    		'search_items'          => __( 'Search Question', 'doublee' ),
    		'not_found'             => __( 'Not found', 'doublee' ),
    		'not_found_in_trash'    => __( 'Not found in Trash', 'doublee' ),
    		'featured_image'        => __( 'Featured Image', 'doublee' ),
    		'set_featured_image'    => __( 'Set featured image', 'doublee' ),
    		'remove_featured_image' => __( 'Remove featured image', 'doublee' ),
    		'use_featured_image'    => __( 'Use as featured image', 'doublee' ),
    		'insert_into_item'      => __( 'Insert into question', 'doublee' ),
    		'uploaded_to_this_item' => __( 'Uploaded to this question', 'doublee' ),
    		'items_list'            => __( 'Items list', 'doublee' ),
    		'items_list_navigation' => __( 'Items list navigation', 'doublee' ),
    		'filter_items_list'     => __( 'Filter items list', 'doublee' ),
    	);
    	$rewrite = array(
    		'slug'                  => 'question',
    		'with_front'            => true,
    		'pages'                 => true,
    		'feeds'                 => true,
    	);
    	$args = array(
    		'label'                 => __( 'Question', 'doublee' ),
    		'description'           => __( 'Frequently Asked Questions', 'doublee' ),
    		'labels'                => $labels,
    		'supports'              => array( 'title', 'editor', 'revisions', 'page-attributes' ),
    		'taxonomies'            => array( 'question_category' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 9,
    		'menu_icon'             => 'dashicons-search',
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => 'faq',
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'rewrite'               => $rewrite,
    		'capability_type'       => 'post',
    	);
    	register_post_type( 'question', $args );
    
    }
    add_action( 'init', 'doublee_question', 0 );

    Thanks again for your suggestions 🙂

    In that case, we will need bit more digging.

    1. Could you try using default WordPress pagination in place of ‘apso_pagination’ call?

    2. Is the CPT being registered by a plugin or by the theme? Try to see if there is a function hooked into ‘pre_get_posts’ (or any other related) filter that is somehow messing with the paged parameter.

    3. See whether the call to register_post_type for this CPT differs from the call to register_post_type for other CPTs

    That is all I can think of at the moment.

    Wait a minute. I see the rwrite->slug parameter is ‘question’.
    So shouldn’t you be looking at /question for the archives instead of /faq ?

    Also, is there a chance that you have a page as well with slug as ‘/faq’.
    If yes, then that is the problem.

    1. Could you try using default WordPress pagination in place of ‘apso_pagination’ call?

    Yep, tried that…page 1 doesn’t show any pagination (presumably because is_paged() returns false) but page 2 and onwards do.

    2. Is the CPT being registered by a plugin or by the theme? Try to see if there is a function hooked into ‘pre_get_posts’ (or any other related) filter that is somehow messing with the paged parameter.

    A custom plugin (that I wrote myself) that doesn’t do much other than register CPTs and custom taxonomies. I just double checked and there’s no pre_get_posts in there.

    Wait a minute. I see the rwrite->slug parameter is ‘question’.
    So shouldn’t you be looking at /question for the archives instead of /faq ?

    /question/this-is-my-question is the URL for a single question. has_archive is set to faq…is that incorrect?

    Also, is there a chance that you have a page as well with slug as ‘/faq’.
    If yes, then that is the problem.

    There used to be one but it’s been trashed and permanently deleted.

    /question/this-is-my-question is the URL for a single question. has_archive is set to faq…is that incorrect?

    The rewrite->slug and has_archive parameters always get me.
    What do you see when you go to YOUR_SITE_URL/question

    There used to be one but it’s been trashed and permanently deleted.
    I am assuming that you have flushed the permalinks after trashing the page?

    Also, have you called flush_rewrite_rules() in your code after the call to register_post_type()?

    What do you see when you go to YOUR_SITE_URL/question

    404.

    I am assuming that you have flushed the permalinks after trashing the page?

    Yep.

    Also, have you called flush_rewrite_rules() in your code after the call to register_post_type()?

    Hadn’t thought of that! Just tried it though, and no luck 🙁

    It is very important when you register your CPT and when you flush the rewrite rules.
    Too early or too late in the WordPress initialization order will not work properly.

    See here to know the right place to do this stuff.

    Even if that does not work, try these steps:
    1. Comment the ‘rewrite’ parameter in the call to register_post_type
    2. Change 'has_archive' => 'faq' to 'has_archive' => true

    I am sure one of these steps is definitely going to fix your problem

    Thanks, I will try those when I am back in the office on Monday and report back 🙂

    Change ‘has_archive’ => ‘faq’ to ‘has_archive’ => true

    This did the trick. Thanks so much again for your help!

    (Note for completeness: I changed the rewrite slug to 'faq' rather than 'question' so that my archive would still be example.com/faq and a side effect of that is that the single questions are now example.com/faq/my-question instead of example.com/question/my-question, which is fine for this particular site but something to note if anyone else is trying to solve this issue.

    Great!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Custom post type pagination problem’ is closed to new replies.