1

Having read 'don't use query_posts', I've also read that it's ok to use 'for displaying a list of posts or custom-post-type posts on a page (using a page template), and you want to make pagination of those posts work'.

So, in wanting to use it for exactly that, my code is:

<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("&post_type=host&showposts=10&orderby=title&order=asc&paged=$page");
while (have_posts()) : the_post();?>

<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endwhile ?>

Not being smart enough to know, I'm wondering if there's a better alternative.

3
  • Is this a secondary loop or do you want to alter the main loop? In other words - do you want something to be displayed in addition to what is currently displayed or change what is currently displayed.
    – kraftner
    May 17, 2017 at 15:21
  • @kraftner... I guess it's a secondary loop, used to display the cpt entries... after a normal loop which displays the page content entered through the edit screen.
    – glvr
    May 17, 2017 at 16:18
  • Possible duplicate of When to use WP_query(), query_posts() and pre_get_posts May 17, 2017 at 22:32

3 Answers 3

1

Thanks to those who've suggested fixes.

I couldn't get my normal pagination working with either, but did find an alternate solution which seems to work and which I'll include here as possible help to others (because I've seen various requests for similar).

<?php
$wp_query = new WP_Query();
$wp_query->query('post_type=host&sort_by=title&order=ASC&showposts=1&paged='.$paged    );
get_template_part( 'page-templates/partial/paginate' );

while ($wp_query->have_posts()) : $wp_query->the_post();
echo "<p>";
the_title();
echo "</p>";
endwhile;

get_template_part( 'page-templates/partial/paginate' );
wp_reset_postdata();
?>

The contents of the included partial page template are:

<?php
// Inserts block of numbered links to other posts, on posts/post summaries and search pages.

if (function_exists("pagination")) {pagination($additional_loop->max_num_pages);}

?>

And the function is:

function pagination($pages = '', $range = 3)
{
$showitems = ($range * 2)+1;

global $paged;
if(empty($paged)) $paged = 1;

if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}

if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo " <a href='".get_pagenum_link(1)."'>&laquo; First</a> ";
if($paged > 1 && $showitems < $pages) echo " <a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a> ";

for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&(!($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems))
{
 echo ($paged == $i)? "<span class=\"current\"> ".$i." </span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\"> ".$i." </a>";
}
}

if ($paged < $pages && $showitems < $pages) echo " <a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a> ";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems <     $pages) echo " <a href='".get_pagenum_link($pages)."'>Last &raquo;</a> ";
echo "</div><!-- /pagination -->\n";
}
}

I don't claim credit, nor pretend to understand enough about how/why this works. And I'm puzzled that in the query, if '$wp_query' is renamed (to something like 'the_query') the pagination vanishes.

7
  • this is almost right, you also need to use different page number parameter than the one wordpress uses, but for pages I guess it should work (will break in archive pages). As to why it works.... it basically recreates the whole paging functionality. May 22, 2017 at 7:08
  • @ Mark Kaplun ... thanks. If you're able to explain 'this is almost right, you also need to use different page number parameter than the one wordpress uses' I'll be grateful.
    – glvr
    May 22, 2017 at 14:52
  • this code uses the paged parameter in the url, but that parameter is a wordpress parameter used for paging (actually forgot right now if archive paging or post paging) and wordpress will use it to change what is displayed in the main loop. Therefor it is better in theory to have your own parameter... the downside is that you will have to rewrite the page calculation functions as well. May 22, 2017 at 15:13
  • ... another thing to look for is what is the canonical URL for those pages, if wordpress set the canonical to the first page for all, the other "paged" pages might not be indexed by google. May 22, 2017 at 15:15
  • @ Mark Kaplun ... thanks, again. I don't know how to change that parameter or the page calculation function (which is code from elsewhere), so I'm searching accordingly. On the canonical issue, yes the paged pages have the same url... won't they be indexed through the on-page links?
    – glvr
    May 23, 2017 at 6:11
0

As you should never use query_posts(), you can try the WP_Query:

<?php 
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query( array(
        'post_type' => 'host',
        'posts_per_page' => 10,
        'order_by' => 'title',
        'order' => 'ASC',
        'paged' => $paged,
        )
    );
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) {
        $my_query->the_post();?>
        <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p><?php
    }
    wp_reset_postdata();
}?>

This acts just like your code, without having to use query_posts().

3
  • Thanks. Seems to not have pagination, and haven't been able to find a way to add the pagination I normally use.
    – glvr
    May 18, 2017 at 4:58
  • @gulliver You didn't mention that in the question, so i just wrote an alternative to query_posts for you. What kind of pagination do you normally use? is it a plugin?
    – Johansson
    May 18, 2017 at 13:47
  • @ Jack Johansson... Thanks. Delayed answering so I could experiment. Haven't been able to get pagination working with your code, but now have alternate code which seems to work and for which I'll add an answer to my own question and include my pagination code.
    – glvr
    May 22, 2017 at 6:04
0

Put this code past your main loop:

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$query_args = array(
    'post_type' => 'post',
    'posts_per_page' => 10,
    'paged' => $paged,
    'orderby' => 'title',
    'order' => 'ASC',
    );

$custom_query = new WP_Query( $query_args );

if ( $custom_query->have_posts() ) { ?>
    <div><?php
    while ( $custom_query->have_posts() ) { 
        $custom_query->the_post(); ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br><?php 
    } ?>
    </div><?php
    // Pagination
    if ($custom_query->max_num_pages > 1) { ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( '<br>Previous', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( '<br>Next' ); ?>
            </div>
        </nav><?php 
    } // End of Pagination
} else { ?>
    <p><?php echo 'No posts found'; ?></p><?php
}
wp_reset_postdata();
?>

It provides pagination, as well. You may change tags to make it compatible with your page format.

4
  • Thanks. The pagination seems to be just 'prev/next'' rather than the 'numbered' I currently use (but didn't make clear in my question).
    – glvr
    May 18, 2017 at 4:43
  • Update - sorry, clumsy comment of mine which I couldn't edit. I now need to find a way to add the numbered pagination I normally use.
    – glvr
    May 18, 2017 at 7:08
  • Replace the navigation block of code with whatever you normally use. Just remember to reference $custom_query in it. May 18, 2017 at 16:05
  • Thanks. Delayed answering so I could experiment. Haven't been able to get pagination working, but now have alternate code which seems to work and for which I'll add an answer to my own question.
    – glvr
    May 22, 2017 at 6:02

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.