Created
September 9, 2014 21:13
-
-
Save mattkelley/a65c1c3c649f9f540676 to your computer and use it in GitHub Desktop.
transitentcaching
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
function me2_nav_get_cities() { | |
$cache_key = 'me2_navigation_cities'; | |
/* Cache length in hours */ | |
$cache_time = 1; | |
// For Debug | |
//delete_transient( $cache_key ); | |
$output = get_transient( $cache_key ); | |
if ( false === ( $output ) ) { | |
$city_args = array( | |
'numberposts' => -1, | |
'orderby' => 'date', | |
'order' => 'ASC', | |
'post_type' => 'city', | |
); | |
$cities = get_posts( $city_args ); | |
$output = array(); | |
foreach ( $cities as $city ) { | |
$status = get_field( 'status', $city->ID ); | |
// Skip 2012 cities | |
if ( 0 == $status ) { | |
continue; | |
}; | |
$object = new StdClass(); | |
$args = array( | |
'ID' => $city->ID, | |
'status' => $status, | |
'permalink' => get_permalink($city), | |
'artist_name' => get_field( 'artist', $city->ID ), | |
'show_date' => get_field( 'date', $city->ID ), | |
'location' => get_field( 'location', $city->ID ) | |
); | |
// Loop through parameters adding them to the object | |
foreach( $args as $k => $v ){ | |
$object->$k = $v; | |
} | |
// Add each object to output array | |
$output[] = $object; | |
} | |
// Set transient for X hour | |
set_transient( $cache_key, $output, $cache_time * HOUR_IN_SECONDS ); | |
} | |
if( isset($output) ) return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment