Since: 2.0
add
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
Parameters
Parameter | Type | Details |
---|---|---|
$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 = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
$email = $_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;
}