I have been trying various methods for the better part of the past three days, and I’ve crawled through innumerable replies here on StackOverflow and WordPress support. Is it possible to have pagination in my plugin that lists posts for an administrator?
My plugin displays posts for an administrator so they can view certain details and add/edit specific options for each page. I realize this could be done using meta data on each individual page but I need to have it done this way: Load, let’s say, 25 posts at a time, and allow the admin to add minor details about each one.
I’ve tried every variation (I say every but let’s just say it’s been every variation I can find on the top 20 pages or so of Google for all kinds of search phrases) using previous_posts_links
, paginate_links()
, etc; I’ve tried using custom wp_query()
functions, instantiating a new wp_query and restoring it when I’m done (not recommended) and have avoided altogether using query_posts()
. I’ve tried adding pre_get_posts()
functions – but on that point while get_posts()
might be faster than running a new wp_query()
, apparently it’s even more difficult to use with paginating it’s results.
The code I’ve tried include all manner of variations of this idea:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query(
array(
'number' => -1,
'posts_per_page' => '25',
'paged' => $paged
) );
while ($my_query->have_posts()) : $my_query->the_post();
echo "<div style='border:2px groove black; margin-bottom:5px;'><h3 class='postlist'>";
the_title();
echo "</h3></div>";
endwhile; ?>
<nav>
<?php previous_posts_link('« Newer', $my_query->max_num_pages) ?>
<?php next_posts_link('Older »', $my_query->max_num_pages) ?>
</nav>
(I’ve trimmed out the options saving stuff so it’s easier to read). Noting this bit: $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
I’ve also tried it as ‘page’ rather than ‘paged’ and that doesn’t have any effect.
This function displays what I need: the amount of posts I’m looking to load, and it seems to get the pagination correctly, and URLs for the pagination correct, but clicking on ANY pagination link generated does nothing.
I’ve tried Chris Coyier’s fix regarding $temp = wp_query... etc
and then restoring that but that doesn’t work either.
Any ideas to get this working? Thank you so much in advance! I’ve been ripping my hair out for hours every day and getting almost nowhere.
EDIT: Also this example (below) pulled directly from the Codex, produces the numbered links, but while they have the correct path/URL for pagination don’t actually progress to Page 2, Page 3, or so on.
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
) );
It gives me the links for 1, 2, 3 Next – but even thought the url appends &page=2
the links don’t work.
may be your problem then – you could simply try…
your pagination links need to refer to paged then rather than page.