What I am looking to do:
Setup WP_Query
pagination in a single-custom-post-type.php template file
What I have done
1) Created a post type called "authors". Each post within that post type is an individual author.
2) Standard edit post screen pages contain a dropdown which lists all posts (authors) from the authors custom post type.
3) Created a single-authors.php template which query's all posts with the author metadata from the dropdown, so the result is a list of posts that have the same author assigned to them (kind of like an archive):
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
global $paged;
/*We need this here to add and maintain Pagination if Template is assigned to Front Page*/
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'posts_per_page' => 10,
'meta_key' => 'author_select',
'meta_value' => $author_id,
'paged' => $paged,
);
$temp = $wp_query;
$wp_query = NULL;
$wp_query = new WP_Query($args);
?>
<?php if( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php // Successfully outputs the results of the above query, so I've omitted the code from this example. ?>
<?php endwhile; ?>
<div class="single_navigation">
<?php if( get_adjacent_post( false, '', true ) ) { ?>
<span class="prev"><?php previous_post_link( '< %link' ) ?></span>
<?php } ?>
<?php if( get_adjacent_post( false, '', false ) ) { ?>
<span class="next"><?php next_post_link( '%link >' ) ?></span>
<?php } ?>
</div><!--/single navigation-->
<?php endif; ?>
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
What I am stuck on
The pagination links do not appear. I have done some research and have found that they use the $wp_query
variable, however when I changed my query variable to $wp_query
, the links appeared, but when clicked did nothing.
Any idea where I am going wrong?
Edit:
To answer some of your questions, my query successfully outputs the posts that I am querying, $author_id already has a value that I did not include in this code snippet. Similarly, I left out the actual content output within the while loop because that part is not the issue. The issue is that I need to paginate the content that already exists on this single post.
The purpose of this functionality was to allow posts to have custom authors (outside of the built in Users system) and this template's purpose is to output all of the posts for a given author (which is already working).
the_title()
'meta_value' => $author_id
being filled from the dropdown mentioned? And when selecting each author via the dropdown, are the first ten posts showing correctly?$author_query->have_posts()
returnstrue
and that the$author_query
actually contains any posts?