0

I have regular posts and one custom post type ('SS13') pulled in together on the same page. When you click through to the 'single' page for each the next_post_link and previous_post link only work within their post group (regular posts or 'SS13'). I can't find a solution so that next/previous work across all the posts. CPT is used for organisational purposes in the admin section.

<?php /* Start loop */ ?>
        <?php while (have_posts()) : the_post(); ?>
            <div class="small-10 large-centered columns">
                <article <?php post_class('standard') ?> id="post-<?php the_ID(); ?>"><!-- 'Standard' added to the post class for style -->

                    <div class="row banner">
                        <div class="large-12 columns">
                            <?php if(get_field('standard_banner_image')){ ?>
                            <img src="<?php the_field('standard_banner_image'); ?>" alt="Banner Image" />
                            <?php } ?>
                        </div>
                    </div><!-- end .row -->
                </article>
            </div>

            <!-- Post Navigation -->
            <div class="row">
                <div class="small-12 large-centered columns">
                    <div class="navigation">
<p class="center"><?php next_post_link('%link', '<span class="nextprevious">Next Post:</span>&nbsp;%title', 'FALSE'); ?>&nbsp&nbsp;|&nbsp;&nbsp;<?php previous_post_link('%link', '<span class="nextprevious">Previous Post:</span>&nbsp;%title',  'FALSE'); ?></p>
</div>
                </div>
            </div><!-- end of .row -->
             <?php endwhile; // End the loop ?>

Any help would be great. Thank You.

2
  • The snippet of code you posted is not enough context to receive a satisfactory answer. Please post your entire loop. Commented Jul 16, 2013 at 12:39
  • Thanks @JohannesPille - have updated in my original post.
    – robin174
    Commented Jul 16, 2013 at 12:55

1 Answer 1

1

You will need a filter on get_{$adjacent}_post_where to add your CPT.

function alter_adj_where_wpse_106694($where) {
  $where = str_replace("p.post_type = 'post'","p.post_type IN ('post','yourcpt')",$where);
  return $where;
}
add_filter('get_next_post_where', 'alter_adj_where_wpse_106694' );
add_filter('get_previous_post_where', 'alter_adj_where_wpse_106694' );

There is no flexibility to that callback. It is a simple string replace. If this is your site you should be OK, but if this is a plugin or a theme that you are distributing you need a much more complicated callback to try to compensate for the activity of other plugins and/or themes.

3
  • Many thanks for your reply. So I would add the above code to my functions.php file - replacing 'yourcpt' with the correct CPT. Do I then need to alter my 'next_post_link' and 'next_previous_link' in the loop?
    – robin174
    Commented Jul 16, 2013 at 15:31
  • No. The filter will do all the work. You may need to control where this runs though. As is, it will effect every link those functions generate across the whole site.
    – s_ha_dum
    Commented Jul 16, 2013 at 15:32
  • Thanks @s_ha_dum... Has begun to solve the issue. However The 'CPT' are skipping the 'Posts'. So the 'next/previous' ignore the 'Posts' and skip to the next available 'CPT'. The 'Posts' seem to be working ok.
    – robin174
    Commented Jul 16, 2013 at 16:18

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.