Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Work pagination into query WordPress

  • SOLVED

I have this query from my author profile which displays the correct number of posts, but doesn't paginate (wp page numbers doesn't show). I've messed around with it, but am not sure how to correctly implement.

<?php
$authorID = $curauth->ID; {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query= 'author=' . $authorID. '&post_type=crate' . $post_type . '&orderby=post_date&order=desc&showposts=6&paged='.$paged;
query_posts($query);
if ( have_posts() ) : ?><p class="tax-archive-cat">CRATE</p>
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>">
<?php the_post_thumbnail('review-hp'); ?></a>
<?php endwhile; else:
endif;
}
//Reset Query
wp_reset_query(); ?>
<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>


Thanks!

Answers (4)

2011-09-06

Grégory Viguier answers:

Hello.
Try this :
<?php
$authorID = $curauth->ID;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query;
$wp_query= null;
$args= 'author=' . $authorID. '&post_type=crate' . $post_type . '&orderby=post_date&order=desc&posts_per_page =6&paged='.$paged;
$wp_query = new WP_Query( $args );

if ($wp_query->have_posts()) { ?>CRATE
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>">
<?php the_post_thumbnail('review-hp'); ?>
</a>

<?php endwhile;
}

// Navigation
if ( $wp_query->max_num_pages > 1 ) {
if ( function_exists('wp_page_numbers') )
wp_page_numbers();
else { ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link(__( "previous" )); ?></div>
<div class="alignright"><?php next_posts_link(__( "next" )); ?></div>
</div>
<?php }
}

// Retrieve the initial query
$wp_query = null; $wp_query = $temp; ?>


EDIT : showposts is deprecated, use posts_per_page instead.


Jeremy Phillips comments:

Thanks, that code made the page numbers appear, although page links 404. Do you have any advise on how to trouble shoot this? Like I mentioned, this is located in the author.php file. I have a rewrite to change the url from authors to members. The pagination url looks like this:

/members/member-name/page/2.

If this is looking like a tough fix I will either raise the reward or create a new question.

Thanks!


Jeremy Phillips comments:

Oh,just found [[LINK href="http://themehybrid.com/support/topic/customized-authorphp-page-also-with-custom-url-having-pagination-404-problems"]]this article[[/LINK]] where someone was encountering a similar problem.


Grégory Viguier comments:

I finally found something, and it works, no need for a custom loop :

<?php
if (have_posts()) { ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>">
<?php the_post_thumbnail('medium'); ?>
</a>
<?php endwhile;
}

// Navigation
if ( $wp_query->max_num_pages > 1 ) {
if ( function_exists('wp_page_numbers') )
wp_page_numbers();
else { ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link(__( "previous" )); ?></div>
<div class="alignright"><?php next_posts_link(__( "next" )); ?></div>
</div>
<?php }
}
?>


And in your functions.php (in your theme folder) :

function set_author_post_type($query) {
//$post_type = ?????;
if ($query->is_author) {
$query->query_vars['post_type'] = 'crate' . $post_type;
$query->query_vars['posts_per_page'] = 6;
return $query;
}
}
add_action('pre_get_posts', 'set_author_post_type');

This code only needs one thing to work : I don't know how you get your $post_type variable (needed in the set_author_post_type( ) function).


Jeremy Phillips comments:

That worked perfectly, thanks!

Although, this created an issue with other loops running on the same page. I will post a new question to resolve this issue.

2011-09-06

John Cotton answers:

Try changing this line:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

to
$paged = (get_query_var('page')) ? get_query_var('page') : 1;


Jeremy Phillips comments:

That doesn't seem to do it.


Jeremy Phillips comments:

Sorry, I accidentally stripped a line of code out of my post here. Please take another look, I edited my original post. Thanks!

2011-09-06

rizaljohn answers:

Hi,

I guess you're missing posts_per_page in your query arguments. It should look something like this:

$query= 'author=' . $authorID. '&post_type=crate' . $post_type . '&orderby=post_date&order=desc&post_per_page=5&paged='.$paged;

query_posts($query);


Hope that helps.

2011-09-06

Denzel Chia answers:

Hi,

I think you should remove wp_reset_query first and try again. As that function resets the query and it will not have next page.

Thanks.