Created
August 6, 2013 22:53
-
-
Save thinkstylestudio/6169543 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* matches the student academic profile with their school in the Gravity Form | |
* it uses a modified version of the save function from the relations post type | |
* plugin that is meant to work with the GF form of this specific ID | |
*/ | |
add_action( 'gform_post_submission_4', 'sfn_tan_make_post_relation', 10, 2); | |
function sfn_tan_make_post_relation( $entry, $form, $custom_id = 0, $object_ids = array(), $post_types = array(), $append = true ) { | |
global $wpdb; | |
$custom_id = $entry[ "post_id" ]; | |
$object_ids = $entry["2"]; | |
// Object ID is valid ? | |
$custom_id = (int) $custom_id; | |
if ( $custom_id == 0 ) { | |
return false; | |
} | |
// No append ? replace ! delete before ! | |
if ( $append == false ) { | |
rpt_delete_object_relation( $custom_id, $post_types ); | |
} | |
// Always an array ? | |
if ( !is_array($object_ids) ) | |
$object_ids = array( (int) $object_ids ); | |
// Cast values and make unique | |
$object_ids = array_map( 'intval', $object_ids ); | |
$object_ids = array_unique( $object_ids ); | |
// No valid ID ? | |
if ( empty($object_ids) ) | |
return false; | |
// Loop for insert on DB ! | |
foreach( (array) $object_ids as $object_id ) { | |
if ( $object_id == 0 || $object_id == $custom_id ) continue; // No zero, no master/master ! | |
$wpdb->insert( $wpdb->posts_relations, array( 'object_id_1' => $custom_id, 'object_id_2' => $object_id ) ); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment