0
votes

Seems like a simple thing, but I can't seem to understand what's happening.

I need to limit the number of posts to 25, but my wp_query always returns all of the records, and ignores the posts_per_page argument.

Here is my code:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'posts_per_page' => 25,
    'paged' => $paged,
    'post_type' => 'noo_company',
    'industry' => $industry,
);

$the_query = new WP_Query( $args );
$posts = $the_query->posts;

And here is the first part of the output when I do print_r($the_query).

WP_Query Object ( [query] => Array ( [posts_per_page] => 25 [paged] => 1 [post_type] => noo_company [industry] => consulting ) [query_vars] => Array ( [posts_per_page] => -1 [paged] => 1 [post_type] => noo_company [industry] => consulting ...

At first the value for posts_per_page in [query] is ok as I set it, but in the second part [query_vars] it's reset to '-1', and I have no idea why this could be happening.

Thanks in advance for any helpful suggestions.

2
try to wp_reset_query(); before querying - user2917245
already did, it's not helping - DigitalSoulArts
doesnt it work on your own theme without any plugins or on someones else theme with plugins? - user2917245
its someone else's theme - DigitalSoulArts
try to use it in one of default themes. a plugin or theme itself might rewrite query. - user2917245

2 Answers

0
votes

this code is working for me and hopefully will work for you just copy and past the code and install a plugin name as "wp_pagenavi" for page pagination.

if ( have_posts() ) : 
`enter code here`$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'noo_company',
    'posts_per_page' => 5,
    'paged' => $paged
  );
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop 
the_title();
endwhile;
if ($the_query->max_num_pages > 1) { 
if(function_exists('wp_pagenavi')) { wp_pagenavi();} 
} 
else: 
_e('Sorry, no posts matched your criteria.'); 
endif;  
endif;
0
votes

I removed the post_type argument and everything is working fine now, its probably something theme specific, I don't know what exactly was causing the problem, but in any case it's fixed now.