What I need to achieve is to get all comments from all products in Woocommerce.
This is not getting me ANY comments at all...
<?php $comments = get_comments( array( 'post_type' => 'product') ); ?>
However
<?php $comments = get_comments( array( 'post_id' => '4169') );
Gets me comments for particular product ID. How to query ALL comments?
Thanks in advance.
What I need to achieve is to get all comments from all products in Woocommerce.
This is not getting me ANY comments at all...
<?php $comments = get_comments( array( 'post_type' => 'product') ); ?>
However
<?php $comments = get_comments( array( 'post_id' => '4169') );
Gets me comments for particular product ID. How to query ALL comments?
Thanks in advance.
Share Improve this question asked Oct 31, 2013 at 10:08 kromakroma 31 gold badge1 silver badge3 bronze badges3 Answers
Reset to default 2Please try this:
$args = array(
'number' => 100,
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product'
);
$comments = get_comments( $args );
where you can edit the number of comments to your needs.
Debug:
Maybe something is changing it via the pre_get_comments
hook?
To debug it you can check out the SQL query with:
global $wpdb;
printf( '<pre>%s</pre>', $wpdb->last_query );
where you add this directly below the above get_comments()
code snippet.
Also check the edit-comments.php
screen if the comments show up there and their status.
You'll need to loop through all comments and display them. Something like this will do the trick:
$args = array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product'
);
$comments = get_comments( $args );
foreach( $comments as $comment ) :
echo( $comment->comment_author . '<br />' . $comment >comment_content);
endforeach;
You can also pass in the 'number' parameter if you want to specify how many comments to show. Default is null (no limit).
Woocommerce overrides comment queries, which seems to impact WC_Comment_Query and get_comments(), by filtering out shop comments directly in the SQL.
Adding the below before using WC_Comment_Query prevented shop comments from being excluded in my query.
remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );