How to get comments of a specific article ?

Hi, I need get all comments of a specific article, I'm trying with this but not work... :/

 $comments = elgg_get_entities(array(
                            'type' => 'object',
                            'subtype' => 'comment',
                            'container_guid' => $gui_post,
                         ));

Thankss!!!

  • What is happening with that code?  What results are you getting?

    Are you sure $gui_post is a guid?  Is it the container entity?

    Also by default elgg_get_entities will only return 10 results, if you want *all* of them you need to disable the limit (not recommended).

  • Use egg_list_entities() instead and you can get that specific blog's guid right from the url

    blog/view/guid_here/

    And you get something like this, I put the comments in the sidebar just for this example

  • Your code will fetch all the comments on the $gui_post entity. You have to iterate on those comments to output the data like,

    $comments = elgg_get_entities(array(
                                'type' => 'object',
                                'subtype' => 'comment',
                                'container_guid' => $gui_post,
                             ));
    if($comments){
        foreach($comments as $comment){
            echo $comment->description;
        }
    }    

    Or, like cim mentioned, you can use the elgg's listing view if you use elgg_list_entities() instead

    echo elgg_list_entities(array(
                                'type' => 'object',
                                'subtype' => 'comment',
                                'container_guid' => $gui_post,
                             ));

  • Hi, I tested the code but not work, return null, the var $gui_post  equals '126' that is the guid of the article, I 'm doing something wrong ?.

    $comments = elgg_list_entities(array(
                                'type' => 'object',
                                'subtype' => 'comment',
                                'container_guid' => '126',
                             ));

    echo $comments;

     

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking