Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

asidre on "WP_Query ignoring the posts_per_page argument."

$
0
0

I have a custom post type which i want to use pagination
hence i have the code set up like below:

global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
	'post_type' => 'cpt',
	'posts_per_page ' => 5,
	'offset' => 0,
	'orderby' => 'title',
	'order' => 'ASC',
	'paged' => $paged,
	'post_status' => 'publish'
);

$wp_query =new WP_Query($args);
print_r($wp_query->query_vars);

then the loop:

<?php if($wp_query->have_posts()) : ?>
<?php while($wp_query->have_posts() ): $wp_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>

Result of the query_vars:

Array ( [post_type] => cpt [posts_per_page ] => 5 [offset] => 0 [orderby] => title [order] => ASC [paged] => 1 [post_status] => publish [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => )

ultimately I want to properly get the max_num_pages variable, however this is not possible because it brings 10 posts by default and I cannot control this value.
Somehow it seems to be overriding the posts_per_page argument before the sql executes.

If I do
$posts = get_posts($args);
instead of
$wp_query = new WP_Query($args);
$posts = $wp_query->posts

it returns 5 posts correctly. But there is a dilemma which i cannot get the max_num_pages value from it.

Thanks in advance for any tips.


Viewing all articles
Browse latest Browse all 8245

Trending Articles