最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - widget_posts_args not using the number of posts in widget

programmeradmin1浏览0评论

So I'm using the widget_posts_args filter to pull in my custom post type posts inside the core 'Recent Posts' widget.

Here is the code:

add_filter('widget_posts_args', function() {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});

So with the filter above, it is in fact pulling in all the 'post' and 'recipe' custom post types which is what I want, but...

With the filter, it's pulling in the Blog pages show at most in the 'Reading' section of the settings page which is set at 10 .. but the issue is, that it's completely ignoring the Number of posts to show count inside the widget itself.

If I remove the filter, it goes back to using the number inside the widget but I don't get my 'recipe' custom post types

The widget has 5 posts defined, but it's pulling in 10 posts based on Blog pages show at most - Here is the picture for reference:

Here is the widget with the correct title:

Is there a way that I can define inside the filter that is should use the widget number of posts instead?

So I'm using the widget_posts_args filter to pull in my custom post type posts inside the core 'Recent Posts' widget.

Here is the code:

add_filter('widget_posts_args', function() {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});

So with the filter above, it is in fact pulling in all the 'post' and 'recipe' custom post types which is what I want, but...

With the filter, it's pulling in the Blog pages show at most in the 'Reading' section of the settings page which is set at 10 .. but the issue is, that it's completely ignoring the Number of posts to show count inside the widget itself.

If I remove the filter, it goes back to using the number inside the widget but I don't get my 'recipe' custom post types

The widget has 5 posts defined, but it's pulling in 10 posts based on Blog pages show at most - Here is the picture for reference:

Here is the widget with the correct title:

Is there a way that I can define inside the filter that is should use the widget number of posts instead?

Share Improve this question asked Feb 19, 2020 at 21:12 DevSemDevSem 2092 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The problem is that you're not modifying the existing arguments for the widget, you're replacing them:

add_filter('widget_posts_args', function() {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});

The result of that filter will be:

$params = [
    'post_type' => [ 'post', 'recipe' ]
];

So any other arguments, including the default number of posts, are removed.

This is because you're not accepting the original value into your filter. Your callback function needs to accept this as an argument, so that you can modify and return it:

add_filter('widget_posts_args', function( $params ) {
    $params['post_type'] = array('post', 'recipe');
    return $params;
});

This is how filters work. You use a function that accepts the original value, and returns a new value.

发布评论

评论列表(0)

  1. 暂无评论