--
-- wp_attachment__delete/1
--
create or replace function wp_attachment__delete(
  integer
) returns int4 as $$

declare 
    p_attach_item_id        alias for $1;
begin   
    delete from cr_wp_image_attachments
    where exists (select 1 from cr_revisions where revision_id 
      = cr_wp_image_attachments.attach_id 
      and item_id = p_attach_item_id);
    
    delete from cr_wp_file_attachments
    where exists (select 1 from cr_revisions where revision_id 
      = cr_wp_file_attachments.attach_id 
      and item_id = p_attach_item_id);

    delete from cr_item_publish_audit
    where item_id = p_attach_item_id;

    perform content_item__delete(p_attach_item_id);

    return 0;
end;$$ language plpgsql;