Last active
February 14, 2019 08:42
-
-
Save lkraav/f394d721bcfc702aba67eb9966fd4e97 to your computer and use it in GitHub Desktop.
Working with Pods - Advanced Content Types, data save example https://www.meetup.com/Tallinn-WordPress-meetup/events/257955635/
This file contains 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
try { | |
$counts = $client->counts->getCounts( [] ); | |
if ( isset( $counts->type ) && 'count.hash' === $counts->type ) { | |
$pod_name = 'metric'; | |
$pod = pods( | |
$pod_name, | |
[ | |
'orderby' => 'ID DESC', | |
'limit' => 1, | |
] | |
); | |
if ( 1 !== $pod->total() ) { | |
\WP_CLI::error( sprintf( 'Failed to fetch last row, pod %s', $pod_name ) ); | |
} | |
while ( $pod->fetch() ) { | |
$row = $pod->row(); | |
$value = $counts->lead->count; | |
// Your table / data model definition. | |
$data = [ | |
'name' => 'intercom_total_leads', | |
'created' => date( 'Y-m-d H:i:s' ), | |
'author' => 293, | |
'value' => $value, | |
'change' => $value - $row['value'], | |
]; | |
/** | |
* Avoid overwriting `$pod` last row. | |
* | |
* @since 2018.08.03 | |
*/ | |
$pod_new = pods( $pod_name ); | |
$pod_new->save( $data ); | |
\WP_CLI::log( sprintf( '%d %d', $data['value'], $data['change'] ) ); | |
} | |
} | |
} catch ( GuzzleException $e ) { | |
\WP_CLI::error( $e->getMessage() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment