Struggling with elgg_get_entities_from_metadata

In my efforts to have messages and notifications as separate things, I've basically copied the messages plugin and assigned them separate subtypes "messages" and "alerts". This works very well. My problem is that the messages_count_unread/alerts_count_unread functions then need amending. I've tried to adjust the elgg_get_entities_from_metadata call, but just can't get the options right to only count the entities of a particular subtype, I'm not sure whether this is due to the md being denormalised. Here's the vanilla function as provided in core:

function alerts_count_unread() {
 $user_guid = elgg_get_logged_in_user_guid();
 $db_prefix = elgg_get_config('dbprefix');

 // denormalize the md to speed things up.
 // seriously, 10 joins if you don't.
 $strings = array('toId', $user_guid, 'readYet', 0, 'msg', 1);
 $map = array();
 foreach ($strings as $string) {
  $id = get_metastring_id($string);
  $map[$string] = $id;

 $options = array(
//  'metadata_name_value_pairs' => array(
//   'toId' => elgg_get_logged_in_user_guid(),
//   'readYet' => 0,
//   'msg' => 1
//  ),
  'joins' => array(
   "JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid",
   "JOIN {$db_prefix}metadata msg_readYet on e.guid = msg_readYet.entity_guid",
   "JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid",
  'wheres' => array(
   "msg_toId.name_id='{$map['toId']}' AND msg_toId.value_id='{$map[$user_guid]}'",
   "msg_readYet.name_id='{$map['readYet']}' AND msg_readYet.value_id='{$map[0]}'",
   "msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'",
  'owner_guid' => $user_guid,
  'count' => true,

 return elgg_get_entities_from_metadata($options);


Adding 'subtyp[es => 'messages'. to $options doesn't seem to work, I've also tried it with the subtype's id, but no joy. I thought maybe it needs another join, but I can't figure out what the SQL is as it stands in order to work out what I need to adjust.

Any suggestions? (Thanks!)

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