Query product comments

Hi, I’m trying to limit the number of comments that are displayed for a product so it doesn’t all load in the DOM.

I am trying to use the query editor but haven’t been able to make it work. I created a product template and set the display conditions to a product I know has many reviews / comments.

In the query editor, I set

return [
    'post_type'      => 'comment',
    'post_status'    => 'approve',
    'post_parent'    => '{post_id}', 
    'fields'         => 'ids',
    'posts_per_page' => -1,
];

However, doesn’t return a list

  1. What am I doing wrong?
  2. Once the query does work, what dynamic fields would provide the name, date, and content fields? Is this some type of echo?

image

1 Like

Hi @lynxm,

In WordPress, the comment is not a post type, and you CANNOT query the comment by using Bricks QueryEditor. (QueryEditor is only support WP_Query, WP_Term_Query or WP_User_Query)

You can only get the comments by using WP_Comment_Query

Also, if you want to do it by using Bricks Query Loop, the only way is to create a custom Query Loop
Related hooks:

bricks/setup/control_options
bricks/query/run
bricks/query/loop_object

You will still need to create some custom functions so you can output the comment content date etc. inside the loop.

If your main problem is the large amount of comments in a single product, you should be able to separate them by setting pagination in WP Admin > Settings > Discussions

Regards,
Jenn

2 Likes

@itchycode - Thanks Jenn.

That solved the DOM problem. I was definitely over complicating it. To be honest, I haven’t looked at those basic wordpress discussion controls in a very long time and I forgot they were there.

As a low priority nice to have, it’d be great to make the pagination an ajax load more button rather reloading the page. However, it’s definitely not worth the effort at the moment to write those custom queries.

Thanks again!

1 Like