Get entities (type object) by title

Elgg 2.3.14

In this case the Objects have unique title.

How to check if a specific title exists without foreach loop and if so, delete it.

Thank you. 

  • Something like this:

    $query = 'Your unique title';
    
    $options = [
        'type' => 'object',
        'subtype' => 'blog',
        'metadata_name_value_pairs' => [
            [
                'name' => 'title',
                'value' => '%' . $query . '%',
                'operand' => 'LIKE',
            ],
        ],
    ];
    
    $entities = elgg_get_entities($options);
    
    return $entities[0]->delete;
  • Thank you RvR, but it does not work.

    The 'metadata_name_value_pairs'  has no effect. I receive all the records.

    Any idea?

    Thanks

  • Be sure that metadata name must have a name you use on your site.

    $query is case sensitive.

    For Elgg 2 you still can use elgg_get_entities_from_metadata() - try it.

  • The question is about the "title". Is "title" metadata of an "object"?

    Thanks

  • Not a metadata but attribute. Anyway, it's works for me but I'm on Elgg 3.

    You can use it on Elgg 2:

    $query = 'Your unique title';
    
    $options = [
        'type' => 'object',
        'subtype' => 'blog',
        'joins' => ['JOIN ' . elgg_get_config('dbprefix') . 'objects_entity oe ON e.guid = oe.guid'],
        'wheres' => ["(oe.title LIKE '%{$query}%')"],
    ];
    
    $entities = elgg_get_entities($options);
    
    return $entities[0]->delete;

     

  • This works fine.

    Only the $entities[0]->delete should be $entities[0]->delete().

    Thank you very much, we really appreciate your help.