For the past two days I've been trying to debug an issue with WP_Query()
and Pagination.
A website I'm a dev for uses a Wordpress theme (Salient by ThemeNectar) that includes the visual composer plugin.
My job is essentially to make sure the site is just functional, so a while ago I wrote an extension for the visual composer.
This extension uses WP_Query()
to create a new query, looks for posts in specific categories (with a default established),
and then returns the output of that query
Here's the code that renders posts and pagination inside that visual composer component
public function renderPosts( $atts, $content = null ) {
global $post;
setup_postdata($post);
extract( shortcode_atts( array(
'foo' => 5, //default of 5
'categoryslug' => 'news-views' // Currently news and views.
), $atts) );
// For getting the Query variable on a statcci front page, you have
// to use 'page' and not 'paged'. Weird.
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
// Two things needed: Not using the game library,
// Definitely using the supplied slug.
// These are two objects.
$dontUse = get_category_by_slug('game-library');
$catUsed = get_category_by_slug($atts->categoryslug);
// Args for the custom query
$query_args = array(
'posts_per_page' => intval($foo),
'category__not_in' => $dontUse->term_id,
'cat' => $catUsed->term_id,
'page' => $paged
);
$custom_query = new WP_Query($query_args);
$output = '<div id="blogroll">';
while ( $custom_query->have_posts() ) : $custom_query->the_post();
$output .= "<div class='home_post col span_12 clear-both'>";
$output .= "<div class='col span_3'><a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail(get_the_ID(), 'home_post_thumb') . "</a></div>";
$output .= "<div class='col span_9 col_last right-edge'>";
$output .= "<h2 class='home_post_header'>";
$output .= '<a href="' . get_the_permalink() . '">' . get_the_title() . "</a>";
$output .= "</h2>";
$output .= get_the_excerpt();
$output .= '<a class="home-more-link" href="' . get_the_permalink() . '"><span class="continue-reading">Read More</span></a>';
$output .= "</div>";
$output .= "</div>";
endwhile;
wp_reset_postdata();
// Pagination not working, but it outputs just fine?
$output .= '<div id="pagination" class="blogroll-pagination">' . home_pagination($custom_query) . '</div></div>';
wp_reset_query();
return $output;
}
Now, I'm aware that the code to generate the content is just a giant concatenated string, but it's only because the extension function has to return something. At the very end, I output pagination with use of another function defined above.
Here's the function I've created called home_pagination(query)
function home_pagination($query = null) {
/*if ( !$query ) {
global $wp_query;
$query = $wp_query;
} commented out because do I need this? */
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var( 'page' ) ),
'total' => $query->max_num_pages,
'prev_text' => '« Previous',
'next_text' => 'Next »',
) );
return $pagination;
}
Now, this was functioning not a week ago but I can't seem to get it to work now... Here's what I've tried:
- Playing around with
get_query_var('page')
- Using a plugin (Wp_page_navi)
- Changing where and when
wp_reset_postdata()
andwp_reset_query()
are used - Using the
max_num_pages
workaround fornext_posts_link()
andprevious_posts_link()
This is on a static front page and I'm aware that the rules are different with 'page' and 'paged'
Interesting to note: When I change the 'paged' variable in the query args, it returns the proper corresponding page.
I'm trying to figure out why the pagination I'm outputting as a part of this query is doing ABSOLUTELY NOTHING when I try to navigate with it? It's as if it wants to navigate to /page/2 or whichever number, but then immediately redirects to the site itself...
I'm considering not putting the paginate_links()
call in a separate function. There are probably glaring mistakes, but I've been
grinding away at pagination that no longer seems to work. Any ideas on how I can best approach this? Is there something huge I'm missing?