Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got a custom authors page for a multi-author blog.

http://blog.securitytechnologies.com/meet-the-team/

Here's the code I'm using to generate the page (thanks to other folks in the support forums for providing it):

<?php
$excluded = "1,2,3,7,20,21";  // To exclude external admin author IDs
$sql = "SELECT DISTINCT post_author, umeta.meta_value as last_name
FROM $wpdb->posts
JOIN $wpdb->usermeta umeta ON ($wpdb->posts.post_author = umeta.user_id
AND umeta.meta_key = 'last_name')
WHERE post_author NOT IN ($excluded)
AND post_status = 'publish'
AND post_type = 'post'
ORDER BY last_name, post_author
";
$authors = $wpdb->get_results($sql);    
if($authors):
foreach($authors as $author):
?>    

What I can't figure out is how to set a maximum number of authors per page and paginate. I've got WP-PageNavi installed, but I'm not having any luck implementing it here on a custom template page.

Thanks in advance.

share|improve this question
add comment

1 Answer

I doubt you're going to be able to repurpose WP-PageNavi for this, since you're not using the built-in queries at all. You should be able to roll pagination yourself, though, and still fall back on some of WordPress's built-in functionality.

In particular, if your URL matches the pattern /meet-the-team/page/3/, the paged querystring variable will automatically be filled with 3. You can use that, then just loop through the correct authors in your list. Here's a bit of quick code:

<?php
$authors_per_page = 5;
$page_number = preg_replace("/[^0-9]/", "", $_GET["paged"]); // don't trust querystring vars
if (empty($page_number))
    $page_number = 1;

$excluded = "1,2,3,7,20,21";  // To exclude external admin author IDs
$sql = "..."; // SQL unchanged, omitted

$authors = $wpdb->get_results($sql);    
if ($authors) :
    $author_count = count($authors);
    $first_author = ($page_number - 1) * $authors_per_page;
    $last_author = min($first_author + $authors_per_page, $author_count);

    foreach ($i = $first_author; $i < $last_author; $i++) :
?>    

<!-- display author -->

<?php
    endforeach;

    // show previous and next page links
    if ($page_number > 1) {
        echo '<a href="/meet-the-team/page/' . ($page_number - 1) . '/">Previous page</a>';
    }
    if ($last_author < $author_count) {
        echo '<a href="/meet-the-team/page/' . ($page_number + 1) . '/">Next page</a>';
    }

endif;
?>
share|improve this answer
    
I agree with using the paged query var, but if there are a lot of authors maybe it makes more sense to use OFFSET and LIMIT within the query so you're not querying for every author on every request. it would also simplify the loop. –  Milo Feb 25 '12 at 15:00
    
That's a good point, but how many authors could there be? You'd also need to retrieve the total number of authors so you don't give a "Next Page" link if none is needed. –  Nate Cook Feb 27 '12 at 20:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.