fetch()

Fetch an item from a Pod.

Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find(). You can rewind the list back to the start by using reset(). Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id or slug.

Function Definition

public function fetch ( $id = null )

Source File: /pods/classes/Pods.php

Since: 2.0

Parameters

PARAMETERTYPEDETAILS
$id(int)ID or slug of the item to fetch

Returns

(array) An array of fields from the row

Examples

Example 1

<?php
    // We have a "books" Pod with "category" and "the_author" 
    // as single-select relationship fields, related to 
    // "categories" and "authors" Pods
    $params = array(
        'where'   => 't.name LIKE "%rings%"',
        'limit'   => -1  // Return all rows
    );

    // Create and find in one shot
    $books = pods( 'books', $params );

    if ( 0 < $books->total() ) {
        while ( $books->fetch() ) {
?>
        <h2><?php echo $books->display( 'name' ); ?></h2>
        <p>Author: <?php echo $books->display( 'the_author' ); ?></p>
        <br />
        <p>Category: <?php echo $books->display( 'category' ); ?></p>
        <br />
<?php
        } // end of books loop
    } // end of found books

The above example will output:

<h2>The Lord of the Rings</h2>
<p>Author: J. R. R. Tolkien</p>
<br />
<p>Category: Fiction</p>
<br />

Example 2

<?php
// Fetch one specific item by ID, fetch() will return non-empty if it's found.
if ( $pod->fetch( 123 ) ) {
    // We found a my_pod item with the ID 123.
}

// Fetch one specific item by slug, fetch() will return non-empty if it's found.
if ( $pod->fetch( 'my-slug' ) ) {
    // We found a my_pod item with the slug 'my-slug'.
}

Other Helpful Documentation on pods()

add_to()

Add an item to the values of a relationship field.

add()

Add an item to a Pod.

data()

Return an array of all rows returned from a find() call.

delete()

Delete an item from the Pod.

display()

Return the output for a field.

do_magic_tags()

Replace magic tags with their values.

duplicate()

Duplicate an item.

exists()

Whether a Pod item exists or not when using fetch() or construct with an ID or slug.

export_data()

Export data from all items.

export()

Export an item’s data.

field()

Return the value for a field.

fields()

Return field array from a Pod, a field’s data, or a field option.

filters()

Output search filters to be used with find().

find()

Find items of a pod.

first_id()

Return the first item ID.

form()

Embed a form to add / edit a pod item from within your theme.

helper()

Run a helper within a Pod Page or WP Template.

id()

Return the item ID.

import()

Import data.

index()

Return the item name.

last_id()

Return the last item ID.

next_id()

Return the next item ID.

nth()

Fetch the nth state.

pagination()

Display the pagination controls.

position()

Fetch the current position in the loop.

prev_id()

Return the previous item ID.

raw()

Return the raw output for a field.

remove_from()

Remove values from fields.

reset_pod()

Delete all items from the Pod.

reset()

Reset the item position back to the start of the find() list.

row()

Return row array for an item.

save()

Save an item.

template()

Display the page template.

total_found()

Fetch to total number of rows found.

total()

Fetch the total row count.

valid()

Pod object validity.

zebra()

Fetch the zebra switch.