Adrian
Adrian

Reputation: 2012

add a filter to a sidebar widget ,wordpress posts_per_page

Hi Im wondering how to alter the number of posts_per_page. The widget() function in default-widgets.php contains this line..

$r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'cat' => 1, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );

The variable $number is set right before this line as 10 but I would rather insert my own filter for posts_per_page and change it to 1.

But Im unsure how to add a filter to this, im only learning about hooks and filters at the moment. As far as I know there is an array with posts_per_page but I dont know how to change this.

function recent_post_count() {
$query->set('posts_per_page', 1);
}
add_filter( 'widget_posts_args', 'recent_post_count', 6); 

Upvotes: 0

Views: 848

Answers (1)

Denis de Bernardy
Denis de Bernardy

Reputation: 78463

You're using the wrong argument.

function recent_post_count($args) {
  $args['posts_per_page'] = 5;
  return $args;
}
add_filter( 'widget_posts_args', 'recent_post_count'); 

Upvotes: 1

Related Questions