1

The following code (old post is here and author is diggy) is useful, but i'd like display post and page on recent comments widget, not only attachments.

function wpse80087_widget_comments_args( $args )
{
    $args = array( 'number' => 5, 'post_type' => 'attachment', 'status' => 'approve', 'post_status' => 'inherit' );
    return $args;
}
add_filter( 'widget_comments_args', 'wpse80087_widget_comments_args', 10, 1 );

Is it possible?


SOLUTION:

Thanks to Pieter Goosen and Birgire.

First step:

Plugin

Second step:

function.php

function wpse80087_widget_comments_args( $args ) {
    $args = array( 
       'number' => 9, 
       'post_type' => array('attachment', 'post', 'page'),
    );
    return $args;
}
add_filter( 'widget_comments_args', 'wpse80087_widget_comments_args', 10, 1 );

2 Answers 2

0

It does seem that you are already using a custom function that you found from this site. You should credit the original author of that code.

The widget_comments_args filter was introduced in Wordpress3.4. This filter is poorly documented. This filter uses get_comments(), so you can also use the same parameters. Here is the filter in wp-includes/default-widgets.php#L847

847                 /**
848                  * Filter the arguments for the Recent Comments widget.
849                  *
850                  * @since 3.4.0
851                  *
852                  * @see get_comments()
853                  *
854                  * @param array $comment_args An array of arguments used to retrieve the recent comments.
855                  */
856                 $comments = get_comments( apply_filters( 'widget_comments_args', array(
857                         'number'      => $number,
858                         'status'      => 'approve',
859                         'post_status' => 'publish'
860                 ) ) );

There is a problem though in get_comments() as it only accepts one post type, not an array or a string which is actually quite funny. There is away around this.

One of the members of the site @birgire has written a plugin called wp-comments-from-mulitple-post-types to add the ability to get_comments and WP_Comment_Query() to accept multiple post types. You can download his plugin here

After installation, you can now use your code like this

function wpse80087_widget_comments_args( $args ) {
    $args = array( 
       'number' => 5, 
       'post_type' => array('attachment', 'post',' page'),
     );
    return $args;
}
add_filter( 'widget_comments_args', 'wpse80087_widget_comments_args', 10, 1 );

EDIT

It seems that from feedback from the OP that either status or post_status is not working and only returns one post type. When these parameters are removed, everything works as expected. See the updated code above

1
  • 1
    Maybe this should be fixed in core? Aug 4, 2014 at 11:27
0

Use the widget_posts_args to modify the args in the get_comments functions for that default widget.

Something like this:

function wpsites_widget_comments_args( $args ) {
$args = array( 
'post_type'   => array( 'attachment', 'post', 'page'),
'number'      => $number,
'status'      => 'approve',
'post_status' => 'publish'  );

return $args;
}
add_filter( 'widget_comments_args', 'wpsites_widget_comments_args', 10, 1 );

You can use any of the get_comments Parameters otherwise you can copy over the code for the recent comments widget from WordPress > wp-includes > default-widgets.php file and replace the get_comments function with WP_Query to create a custom recent comments widget in your child theme or plugin

3
  • Thank you Brad. Your code doesn't work. I'll try with a custom widget if there aren't solutions. Aug 2, 2014 at 23:51
  • O.K Probably because the get_comments function doesn't accept the parameters you need to modify. You can use those args in the new WP_Query so still useful. Aug 2, 2014 at 23:54
  • Looks like my code is fine but needs the plugin installed first as its a bug in WordPress. github.com/birgire/wp-comments-from-mulitple-post-types/blob/… Aug 4, 2014 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.