Created
June 15, 2017 22:12
-
-
Save matgargano/94cf0e388bdee2ddaed7c32bd19c19a1 to your computer and use it in GitHub Desktop.
acf
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
<?php | |
/** | |
* Save Work ID | |
* | |
* Save work ID on the fly when adding new post. | |
* | |
* @param string $post_id Post ID. | |
* | |
* @return array $_POST Post Data. | |
*/ | |
function prh_save_workid( $post_id ) { | |
if ( 'post' !== get_post_type( $post_id ) ) { | |
return; | |
} | |
$field_name = 'blogpost_bookcontent_repeater'; | |
$value = get_field( $field_name, $post_id ); | |
$new_array = []; | |
if ( ! is_array( $value ) ) { | |
return $value; | |
} | |
foreach ( $value as $repeater ) { | |
if ( ! $repeater['link_family'] ) { | |
$new_array[] = $repeater; | |
continue; | |
} | |
$title = new \PRHAPI\Requests\Data\Titles( $repeater['blogpost_bookisbn_text'] ); | |
$response = $title->get_response_body(); | |
$repeater['work_id'] = $response->data->titles[0]->workId;; | |
$new_array[] = $repeater; | |
} | |
update_field( $field_name, $new_array, $post_id ); | |
} | |
add_action( 'acf/save_post', 'prh_save_workid', PHP_INT_MAX ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment