ORM Basics

Filtering

Top ↑

by( $key, $value = null )

This method is an alias of the where() method.


Top ↑

in( $post_ids )

This method filters posts down to the those whose IDs are within the $post_ids array.

Example:

$events = tribe_events()
    ->in( [ 1, 2, 3 ] )
    ->all();

Top ↑

not_in( $post_ids )

This method filters posts down to the those whose IDs are NOT within the $post_ids array.

Example:

$events = tribe_events()
    ->not_in( [ 1, 2, 3 ] )
    ->all();

Top ↑

parent( $post_id )

Grab posts whose parent post is the given $post_id.

Example:

$events = tribe_events()
    ->parent( 5 )
    ->all();

Top ↑

parent_in( $post_ids )

Grab posts whose parent posts are within the given $post_ids array.

Example:

$events = tribe_events()
    ->parent_in( [ 1, 2, 3 ] )
    ->all();

Top ↑

parent_not_in( $post_ids )

Grab posts whose parent posts are NOT within the given $post_ids array.

Example:

$events = tribe_events()
    ->parent_not_in( [ 1, 2, 3 ] )
    ->all();

Top ↑

search( $value )

Filter results based on a search term.

Example:

$events = tribe_events()
    ->search( 'bork' )
    ->all();

Top ↑

where( $key, $value = null )

This method filters the results by the given key and value. Supported keys are those that are supported by the WP_Query class plus any of the respository’s declared schemas.

Example:

// Filter down to posts with a key and value.
$events = tribe_events()
    ->where( 'post_id', 5 )
    ->all();

// Filter down to posts with a key and default value.
$events = tribe_events()
    ->where( 'has_tickets' )
    ->all();

Top ↑

Ordering Results

Top ↑

order( $order = 'ASC' )

This is a simple modifier that orders results from the repository in ascending (ASC) or descending (DESC) order.

Example:

$events = tribe_events()
    ->order( 'ASC' )
    ->all();

Top ↑

order_by( $order_by, $order = 'DESC' )

You can order by a specific field and in a specific direction using this method.

Example:

$events = tribe_events()
    ->order_by( 'start_date', 'DESC' )
    ->all();

Top ↑

sort( $orderby = [], $order = 'ASC', $preserve_keys = false )

With the sort() method, you can sort by multiple fields in a specific direction.

Example:

$events = tribe_events()
    ->sort( [ 'start_date', 'post_title' ], 'ASC' )
    ->all();

Top ↑

Pagination

Top ↑

next()

Get the next page of results.

Example:

$events = tribe_events()
    ->per_page( 5 )
    ->next()
    ->all();

Top ↑

page()

Set the page number of the results to fetch.

Example:

// Fetches events the second 5 results.
$events = tribe_events()
    ->per_page( 5 )
    ->page( 2 )
    ->all();

Top ↑

per_page()

Set the number of posts per page for the fetched results.

Example:

// Fetches 5 events.
$events = tribe_events()
    ->per_page( 5 )
    ->all();

Top ↑

prev()

Get the previous page of results.

Example:

$events = tribe_events()
    ->per_page( 5 )
    ->prev()
    ->all();

Top ↑

Fetching results

Top ↑

all()

Gets the results as posts.

Example:

$events = tribe_events()
    ->per_page( 20 )
    ->all();

Top ↑

count()

Count the found posts.

Example:

$num = tribe_events()
    ->per_page( 20 )
    ->count();

Top ↑

first()

Get the first post in the result set.

Example:

$event = tribe_events()
    ->per_page( 20 )
    ->first();

Top ↑

get_ids()

Gets the IDs of the posts in the result set.

Example:

$event_ids = tribe_events()
    ->per_page( 20 )
    ->get_ids();

Top ↑

last()

Get the last post in the result set.

Example:

$event = tribe_events()
    ->per_page( 20 )
    ->last();

Top ↑

pluck( $field )

Gets the field from all posts in the result set.

Example:

$event_ids = tribe_events()
    ->per_page( 20 )
    ->pluck( 'post_title' );

Top ↑

Modifying the database

Top ↑

create()

With this method (and ->set_args()), we can create new events.

Example:

$event = tribe_events()
    ->set_args( [
        'title'      => 'My Event',
        'start_date' => '+2 days 15:00:00',
        'duration'   => HOUR_IN_SECONDS,
        'status'     => 'publish',
    ] )
    ->create();

Top ↑

delete()

With this method you can delete events.

Example:

// Delete a specific event.
tribe_events()
    ->where( 'id', 5 )
    ->delete();

// Delete a collection of events.
tribe_events()
    ->where( 'ends_before', 'today' )
    ->delete();

Top ↑

save()

We can update existing data for an event.

Example:

$updated = tribe_events()
    ->where( 'id', 5 )
    ->set( 'title', 'My New Title' )
    ->set( 'category', [ 17, 18 ] )
    ->save();