0

I have run into this issue before with pagination where your custom post type is called something like 'insights' and then the page that you create to display those custom posts in an archive is called 'insights' too. Straight away there is a naming conflict. When WordPress searches for the second page it actually searches the page 'insights' instead of the post type 'insights' and of course it returns a 404 not found error. Just to double check I have changed the name of the page on my local and sure enough pagination worked.

The problem I have is that I can't just rename the page to something else because that is out of my hands (unless it is the only way) and their are hundreds of posts so I can't just rename the post type. So based on my code below is there anything I can change to fix this in code?

<?php
/*
    Template name: Insights
*/      

get_header();
the_post();
$insights = get_field('insights');
$filters = array();

// Category
if( isset($_GET['insights_category']) && $_GET['insights_category'] != 0 ) {
    $filters[] = array(
        'taxonomy'  => 'insights_category',
        'field'     => 'id',
        'terms'     => $_GET['insights_category']
    );
}

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$insights = new WP_Query(array(
    'post_type' => 'insights',
    'post_status' => 'publish',
    'order' => 'DESC',
    'tax_query' => $filters,
    'posts_per_page' => 9,
  'paged' => $paged
));

?>

<?php echo get_template_part('content', 'utility-bar'); ?>

<div class="nine">
    <!-- Display the Page's Content in a div box. -->
    <div class="entry">
        <h2><?php the_title(); ?></h2>
        <?php echo get_template_part('content', 'description'); ?>
    </div>
    <div class="borderEntry"></div>

<?php /*
???
    <div class="content-landing box" >
        <?php the_content(); ?>
    </div>
*/ ?>

    <div id="insights_main">
        <?php
        if ($insights->have_posts()) :
            while( $insights->have_posts() ) :
                $insights->the_post();
        ?>
            <div class="insights_content">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <div class="insights_meta">
                    Posted on <?php the_time('F j, Y \a\t g:i a'); ?>
                    <?php
                        $tags = wp_get_post_terms( $post->ID, 'insights_tag');
                        if ($tags) {
                            echo ' | ';
                            $i = 1;
                            $last = count($tags);
                            foreach ($tags as $tag) {
                                echo '<a href="/' . $tag->taxonomy . '/' . $tag->slug . '">' . $tag->name . '</a>';
                                if ($i < $last) {
                                    echo ', ';
                                }
                                $i++;
                            }
                        }
                    ?>
                </div>
                <div class="isights_img">
                    <?php if (has_post_thumbnail()): ?>
                        <?php the_post_thumbnail(); ?>
                    <?php else:?>
                        <img src="<?php echo get_template_directory_uri(); ?>/images/web/default-people.png" alt=" " />
                    <?php endif; ?>
                </div>
                <div class="insights_excerpt">
                    <?php the_excerpt(); ?>
                </div>
                <div class="insights_excerpt_bottom">
                    <div class="insights_social">
                        <a href="https://twitter.com/intent/tweet?url=<?php the_permalink(); ?>&text=<?php the_title(); ?>" class="insights_twitter"></a>
                        <a href="https://facebook.com/sharer.php?u=<?php the_permalink(); ?>" class="insights_facebook"></a>
                        <a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" class="insights_gplus"></a>
                        <a href="http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink(); ?>&title=<?php the_title(); ?>" class="insights_linkedin"></a>
                        <a href="mailto:?subject=<?php the_title(); ?>&body=<?php the_permalink(); ?>" class="insights_email"></a>
                    </div>
                    <a class="insights_more" href="<?php the_permalink(); ?>">Read more →</a>
                </div>
            </div>
            <div class="border-bottom"></div>

            <?php endwhile; ?>

      <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts', $insights->max_num_pages ); ?></div>
      <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts', $insights->max_num_pages ); ?></div>

        <?php else: ?>
            <div class="insights_content">
                <p>No insights post matches your selected criteria.</p>
                <p>Please try your search with different criteria, or <a href="<?php echo get_template_directory_uri(); ?>/insights/">view all insights posts</a>.</p>
            </div>
            <div class="border-bottom"></div>
        <?php endif; ?>
    </div>

  <?php wp_reset_postdata(); ?>

  <?php require 'tpl-insights-right-menu.php'; ?>
</div>

<?php get_footer(); ?>
4
  • Why not use the automatically generated post type archive instead of a page?
    – Milo
    Commented Sep 6, 2015 at 23:56
  • Well the site is fairly well established and that is how it was originally set up, to use a page and custom template. What would changing it involve? Creating a new php file like 'archive-insights.php' and copying the markup?
    – chap
    Commented Sep 7, 2015 at 0:06
  • The template code would change, as the default loop would contain the custom posts instead of the page data. You would also have to modify the post type registration code to add the has_archive argument.
    – Milo
    Commented Sep 7, 2015 at 0:10
  • I just investigated this and it appears I can't use the built in archive because they are using custom fields, so they can optionally add a slider to the page. Also, with a custom post type archive there doesn't seem to be a native option to add it to the menu. Is there another way, or will I have to rename the slug?
    – chap
    Commented Sep 7, 2015 at 0:50

1 Answer 1

3

You can add a rewrite rule to override the post type rules and force it to load your page.

function wpd_fix_insights_pagination(){
    add_rewrite_rule(
        'insights/page/([0-9]+)/?$',
        'index.php?pagename=insights&paged=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpd_fix_insights_pagination' );

Visit your Settings > Permalinks admin page after adding this code to flush rewrite rules.

5
  • This did fix the 404 not found error, so it does go to page/2 successfully. However, it doesn't seem to load the new posts. The old posts are still there and the newer posts link doesn't show up, it just shows the older posts link.
    – chap
    Commented Sep 7, 2015 at 1:41
  • If you echo get_query_var('paged'), what do you see? I did a quick test in a page template and I get the correct value. As long as you're correctly passing that to the query, I'm not sure why it wouldn't be working.
    – Milo
    Commented Sep 7, 2015 at 1:58
  • It's start at 0 and then it is 2 when I click older posts
    – chap
    Commented Sep 7, 2015 at 2:02
  • var_dump( $insights ) after the query is run and you will see how all of the query vars are set, it may give you some indication as to why it's not working.
    – Milo
    Commented Sep 7, 2015 at 2:03
  • I was doing $paged = ( get_query_var('page') ) ? get_query_var('page') : 1; instead of $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; which gets the single post pagination, rather than for listings. Thanks for your help Milo!
    – chap
    Commented Sep 7, 2015 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.