Post emergency WordPress questions for fast help... ...Or answer questions first & win prize money.

$20
Help paginating a custom query

I have a custom post display page like this:

http://www.activejunky.com/category/clothing-footwear/mens-clothing/mens-jackets

where posts (products) are displayed for a given category, in which case my custom pagination function works, but then when the brand name is set as a restriction, like here:

http://www.activejunky.com/category/clothing-footwear/mens-clothing/mens-jackets?brandName=The+North+Face&order=DESC

...then the pagination fails (though the product count shown above the grid is correct), and we see my set WP default of 39 posts.

My queries for the products are as follows:


<?php
$brandName= $_GET['brandName'];
$order= $_GET['order'];

if (isset($brandName)) {
} else {
$brandName = "ALL";
}

if (isset($order)) {
} else {
$order = "DESC";
}

global $wp_query;

if ($brandName=="ALL") {

$meta_key= "Retail_Price";
$orderby= "meta_value_num";
query_posts('numberposts=-1&paged='.$paged.'&meta_key='.$meta_key.'&orderby='.$orderby.'&order='.$order.'');
$total_results = $wp_query->found_posts;


} else {

$post_ids = array();
$meta_key = 'product_brand';
$unordered_posts = get_posts('paged='.$paged.'&numberposts=-1&meta_key='.$meta_key.'&meta_value='.$brandName.'');

foreach( $unordered_posts as $unordered_post ) {
$post_ids[]= $unordered_post->ID;
};

query_posts( array( 'post__in' => $post_ids , 'orderby' => 'meta_value_num' , 'order' => $order , 'meta_key' => 'Retail_Price' ) );

$total_results = $wp_query->found_posts;

}
?>


The function for the pagination Im using is:


<?php
function wp_pagenavi($before = '', $after = '', $prelabel = '', $nxtlabel = '', $pages_to_show = 20, $always_show = false)
{
global $request, $posts_per_page, $wpdb, $paged;
if (empty($prelabel)) {
$prelabel = '&laquo; previous page';
}
if (empty($nxtlabel)) {
$nxtlabel = 'next page &raquo;';
}
$half_pages_to_show = round($pages_to_show / 2);
if (!is_single()) {

if (!is_category()) { // brand search page matches this cond
preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
} else {
preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
}
$fromwhere = $matches[1];
$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
$max_page = ceil($numposts / $posts_per_page);
if (empty($paged)) { // brand search page matches this cond
$paged = 1;
}
if ($max_page > 1 || $always_show) {

//echo "$before <span id='howManyPages'>Pages ($max_page): </span>";
echo "$before ";
if ($paged >= ($pages_to_show - 1)) {
echo '<a href="' . get_pagenum_link() . '" class="firstPageLink">&laquo; first</a> ';
}

previous_posts_link($prelabel);

echo "<span id='howManyPages'>Pages ($max_page): </span>";

for ($i = $paged - $half_pages_to_show; $i <= $paged + $half_pages_to_show; $i++) {
if ($i >= 1 && $i <= $max_page) {
if ($i == $paged) {
echo "<strong class='on'>$i";
} else {
echo ' <a href="' . get_pagenum_link($i) . '">' . $i . '</a> ';
}
}
}
next_posts_link($nxtlabel, $max_page);
if (($paged + $half_pages_to_show) < ($max_page)) {
echo ' <a href="' . get_pagenum_link($max_page) . '" class="lastPageLink">last &raquo;</a>';
}
echo " $after";
}
}
}
?>


Im wondering if this function could be adapted to work in both cases on this type of page- I did some testing and it seems that once the brand restriction is applied, the var $numposts returns 1 instead of the true value of the total number of products.

Thanks in advance for any help!


Adam Bundy | 07/30/10 at 12:42pm | RSS Answers | Tags

(4) Possible Answers Submitted...

You must be logged in to view answers or answer this question.