Gary Woods
Gary Woods

Reputation: 1011

posts_per_page does not function

Everything works in my code beside posts_per_page (the number of posts it will display). Here is part of my Wordpress code:

<?php 
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'pinned', // tag filtered
    'post__not_in' => array(get_the_ID())
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>

Why does it not work? I have read the documentation and do not understand what the issue is.

Here is the full code if you want to view and test it, just set a number for $posts_number: http://pastebin.com/uNsynNiM

Upvotes: 0

Views: 733

Answers (1)

maiorano84
maiorano84

Reputation: 11951

From your previous questions, your theme is primarily using query_posts to run any and all Post Queries against your database. Since this alters the main loop, WP_Query and get_posts are also affected by any and all existing calls to query_posts. I'm fairly certain that the WP_Query call in your Widget is being affected by a rogue query_posts call somewhere.

My suggestion, since it would probably take quite a bit of effort to convert your theme from all instances of query_posts to WP_Query, would be to temporarily use 'showposts' since that's what essentially fixes the problem. I would still advise switching all calls to the query_posts function to WP_Query, only because there is a significant increase in performance by doing so, but ultimately, that would require a significant effort on your part to fix a rather small problem.

Personally, would I do it? As an exercise in good practice, I probably would, yes. Would I say it's worth it? It depends on the project. If this is just a personal project, I wouldn't say it's necessary.

Good luck with this, and I hope it all works out.

Upvotes: 1

Related Questions