add()

Add an item to a Pod.

Add an item to a Pod by giving an array of field data or set a specific field to a specific value if you’re just wanting to add a new item but only set one field. You may be looking for save() in most cases where you’re setting a specific field.

Function Definition

public function add ( $data = null, $value = null )

Source File: /pods/classes/Pods.php

Since: 2.0

Parameters

PARAMETERTYPEDETAILS
$data(array|string)Either an associative array of field information or a field name
$value(mixed)(optional) Value of the field, if $data is a field name

Returns

(int) The item ID

Examples

Example 1

<?php
// Get the book pod object
$pod = pods( 'book' );

// To add a new item, let's set the data first
$data = array(
    'name' => 'New book name',
    'author' => 2, // User ID for relationship field
    'description' => 'Awesome book, read worthy!'
);

// Add the new item now and get the new ID
$new_book_id = $pod->add( $data );


// If you're already using Pods for another item
$pod = pods( 'book', 4 );

// You can still an add item without effecting anything
$new_book_id = $pod->add( $data );

Add a new applicant

Real life example of a function to create a new “applicant” record using data submitted from a form.

<?php
function add_new_applicant () {

    // Get field values from form submission
    $first_name = sanitize_text_field( $_POST['first_name'] );
    $last_name = sanitize_text_field( $_POST['last_name'] );
    $telephone = sanitize_text_field( $_POST['telephone'] );
    $email = sanitize_text_field( $_POST['email'] );
    
    $fields = array(
        'first_name'    => $first_name,
        'last_name'        => $last_name,
        'telephone'        => $telephone,
        'email'            => $email
    );
                    
    $new_id = pods( 'applicant' )->add( $fields );
    
    return $new_id;
}

Other Helpful Documentation on pods()

add_to()

Add an item to the values of a relationship field.

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.

fetch()

Fetch an item from a Pod.

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.