Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Created February 2, 2014 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sc0ttkclark/8768847 to your computer and use it in GitHub Desktop.
Save sc0ttkclark/8768847 to your computer and use it in GitHub Desktop.
pods_api_post_save_pod_item_{pod} hook example
<?php
/**
* Filter the Pods saving process and run actions / modify values
*
* @param array $pieces An array of different variables you can *change*, see below
*
* List of $pieces variables includes:
*
* array $pieces['fields'] An array of fields on the Pod, 'value' key stores the *new* value if it's been set to save
*
* object $pieces['params'] An object of parameters sent to PodsAPI::save_pod_item
*
* array $pieces['pod'] An array of information about the Pod, like id, name, label, etc
*
* array $pieces['fields_active'] An array of fields that are currently being saved,
* you must add a new value to this array to have Pods save it, or remove it to not have it save
*
* array $pieces['object_fields'] An array of the WP Object fields for the Pod (WP-based content types)
*
* array $pieces['custom_fields'] An array of the custom fields (ones that aren't actually fields on the Pod)
* being saved, this cannot be changed (meta-based content types)
*
* array $pieces['custom_data'] An array of the custom field values being saved, you can change this to add
* other custom fields to the saving process (meta-based content types)
*/
function my_pod_essay_save( $pieces ) {
$fields = $pieces[ 'fields' ];
if ( !empty( $fields[ 'wp_post_id' ][ 'value' ] ) ) {
// Create update post object
$updated_post = array();
$updated_post[ 'ID' ] = $fields[ 'wp_post_id' ][ 'value' ];
$updated_post[ 'post_date' ] = $fields[ 'date' ][ 'value' ];
//if this is a ya essay, add the ya category
if ( empty( $fields[ 'ya_essay' ][ 'value' ] ) ) {
// Insert category "Essay"
$updated_post[ 'post_category' ] = array( 63 );
}
else {
$updated_post[ 'post_category' ] = array( 63, 203 );
}
// Update the post into the database
wp_update_post( $updated_post );
}
// The pre-save filter allows you to change values, you must return $pieces
return $pieces;
}
add_filter( 'pods_api_post_save_pod_item_my_pod_essay', 'my_pod_essay_save' );
// Also consider using post_save instead, it's an action and is only run on a successful save (after validation, etc)
// add_action( 'pods_api_pre_save_pod_item_my_pod_essay', 'my_pod_essay_save' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment