Skip to content

Instantly share code, notes, and snippets.

@aaroneaton
Last active August 29, 2015 14:17
Show Gist options
  • Save aaroneaton/e7810a5c6e457ab38c6a to your computer and use it in GitHub Desktop.
Save aaroneaton/e7810a5c6e457ab38c6a to your computer and use it in GitHub Desktop.
Storing ArrayObject as WordPress transient
<?php
/** ..... */
public function all() {
if ( false === ( $collection = get_transient( '_optin_cache_' . $this->user->ID . '_all' ) ) ) {
$optins = get_posts(
array(
'post_type' => 'optin_saas',
'author' => $this->user->ID,
'no_found_rows' => true,
'cache_results' => false,
'nopaging' => true
)
);
if ( empty( $optins ) ) {
throw new \Exception( 'Optin forms were not found for this account' );
}
// Create the collection object. Extends ArrayObject.
$collection = new OptinCollection();
// Create the optin object and add to the collection
foreach ( $optins as $optin ) {
$o = new Optin( $this->user );
$collection->append( $o->build_from_wp_post( $optin ) );
}
// Save the collection as a transient
set_transient( '_optin_cache_' . $this->user->ID . '_all', $collection, DAY_IN_SECONDS );
}
return $collection;
}
/** ....... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment