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'm getting products (from a specific category) using a custom query_posts() function. I limited the showposts parameter to 25, so how do I know to activate pagination if there are still products (posts) to load?

My query_posts() code looks as follows:

$args = array(
    'post_type' => 'product',
    'taxonomy' => 'product_cat',
    'term' => $idObj->slug, // category slug
    'showposts' => 25,
    'orderby' => 'title',
    'order' => 'asc',
    'paged' => $paged  // global $paged variable
);

$all_products = query_posts( $args );

And then I output my products using a foreach statement:

foreach ($all_products as $product) {...}

I put this function in my functions.php, but $paged nor $max_pages don't seem to be set. So I get false from this function.

Now, can anyone maybe point me in some direction, as I've got no clue.

share|improve this question
    
I guess showposts is deprecated... You should use posts_per_page... –  Rutwick Gangurde Jun 13 '12 at 11:20
    
I've tried, but it doesn't work as expected. –  Anriëtte Myburgh Jun 18 '12 at 9:23
add comment

1 Answer

up vote 4 down vote accepted

First of all: do not use query_post!

For more information read: When should you use WP_Query vs query_posts() vs get_posts()?

Use the WP_Query class to fetch your products, also pay attention that the showposts argument is deprecated, use posts_per_page instead:

$args = array(
    'post_type' => 'product',
    'taxonomy' => 'product_cat',
    'term' => $idObj->slug, // category slug
    'posts_per_page' => 25,
    'orderby' => 'title',
    'order' => 'asc',
    'paged' => $paged  // global $paged variable
);

$all_products = new WP_Query( $args );

// The Loop
while ( $all_products->have_posts() ) : $all_products->the_post();
    // do stuff here...
endwhile;

// Reset $post global
wp_reset_postdata();

To get the total number of found posts use the $found_posts property. And if you need the total amount of pages use the $max_num_pages property.

$found_posts = $all_products->found_posts;
$max_pages = $all_products->max_num_pages;
share|improve this answer
    
The correct reset function for a custom query via a new WP_Query object is wp_reset_postdata(). Refer to the codex or to Rarst's flowchart you linked to. Fixed. Also, while I was at it, note that "fields of query" are properties. –  Johannes Pille Jun 13 '12 at 12:02
1  
@JohannesPille thanks! –  Eugene Manuilov Jun 13 '12 at 12:07
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.