[Documentation](https://groundhogg.io/documentation/) » [Functions](https://groundhogg.io/documentation/#doc-functions)

# get_db

Published Tuesday, June 9, 2026 · Last updated Tuesday, June 9, 2026

Returns a Groundhogg database table instance by its key name. Each table provides `query()`, `add()`, `update()`, `delete()`, and `count()` methods for direct database access without writing raw SQL. Common table keys include `'contacts'`, `'tags'`, `'tag_relationships'`, `'events'`, `'event_queue'`, `'activity'`, `'emails'`, `'funnels'`, `'broadcasts'`, `'submissions'`, `'notes'`, `'tasks'`, and `'permissions_keys'`.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `string` | The table key. Passing an empty string returns the `DB\Manager`. |

## Return value

`DB\Table|DB\Manager` — A specific table instance, or the `DB\Manager` if `$table` is empty.

## Usage

```

// Query the contacts table
$rows = \Groundhogg\get_db( 'contacts' )->query( [ 'owner_id' => 1 ] );

// Count activity for a contact
$count = \Groundhogg\get_db( 'activity' )->count( [
    'contact_id'    => $contact->get_id(),
    'activity_type' => 'email_opened',
] );
```

## Example

Here’s a pseudo code example for a hypothetical reporting integration.

```

/**
 * Get all contacts who have opened at least one email in the last 30 days.
 *
 * @return \Groundhogg\Contact[]
 */
function my_plugin_get_recently_active_contacts() {

    $since = date( 'Y-m-d H:i:s', strtotime( '-30 days' ) );

    $rows = \Groundhogg\get_db( 'activity' )->query( [
        'activity_type' => 'email_opened',
        'after'         => $since,
    ] );

    $contact_ids = array_unique( wp_list_pluck( $rows, 'contact_id' ) );

    return array_filter( array_map( function( $id ) {
        return \Groundhogg\get_contactdata( $id );
    }, $contact_ids ) );
}
```
