Skip to content

Instantly share code, notes, and snippets.

@thinkstylestudio
Forked from wokamoto/post_meta_transient.php
Last active August 29, 2015 14:23
Show Gist options
  • Save thinkstylestudio/ac83b1fdd316171be5f2 to your computer and use it in GitHub Desktop.
Save thinkstylestudio/ac83b1fdd316171be5f2 to your computer and use it in GitHub Desktop.
<?php
/**
* Delete a post meta transient.
*/
function delete_post_meta_transient( $post_id, $transient, $value = null ) {
global $_wp_using_ext_object_cache;
$post_id = (int) $post_id;
do_action( 'delete_post_meta_transient_' . $transient, $post_id, $transient );
if ( $_wp_using_ext_object_cache ) {
$result = wp_cache_delete( "{$transient}-{$post_id}", "post_meta_transient-{$post_id}" );
} else {
$meta_timeout = '_transient_timeout_' . $transient;
$meta = '_transient_' . $transient;
$result = delete_post_meta( $post_id, $meta, $value );
if ( $result )
delete_post_meta( $post_id, $meta_timeout, $value );
}
if ( $result )
do_action( 'deleted_post_meta_transient', $transient, $post_id, $transient );
return $result;
}
/**
* Get the value of a post meta transient.
*/
function get_post_meta_transient( $post_id, $transient ) {
global $_wp_using_ext_object_cache;
$post_id = (int) $post_id;
if (has_filter('pre_post_meta_transient_' . $transient)) {
$pre = apply_filters( 'pre_post_meta_transient_' . $transient, $post_id, $transient );
if ( false !== $pre )
return $pre;
}
if ( $_wp_using_ext_object_cache ) {
$value = wp_cache_get( "{$transient}-{$post_id}", "post_meta_transient-{$post_id}" );
} else {
$meta_timeout = '_transient_timeout_' . $transient;
$meta = '_transient_' . $transient;
$value = get_post_meta( $post_id, $meta, true );
if ( $value && ! defined( 'WP_INSTALLING' ) ) {
if ( get_post_meta( $post_id, $meta_timeout, true ) < time() ) {
delete_post_meta_transient( $post_id, $transient );
return false;
}
}
}
return
has_filter('post_meta_transient_' . $transient)
? apply_filters( 'post_meta_transient_' . $transient, $value, $post_id )
: $value;
}
/**
* Set/update the value of a post meta transient.
*/
function set_post_meta_transient( $post_id, $transient, $value, $expiration = 0 ) {
global $_wp_using_ext_object_cache;
$post_id = (int) $post_id;
delete_post_meta_transient( $post_id, $transient );
if (has_filter('pre_set_post_meta_transient_' . $transient))
$value = apply_filters( 'pre_set_post_meta_transient_' . $transient, $value, $post_id, $transient );
if ( $_wp_using_ext_object_cache ) {
$result = wp_cache_set( "{$transient}-{$post_id}", $value, "post_meta_transient-{$post_id}", $expiration );
} else {
$meta_timeout = '_transient_timeout_' . $transient;
$meta = '_transient_' . $transient;
if ( $expiration ) {
add_post_meta( $post_id, $meta_timeout, time() + $expiration, true );
}
$result = add_post_meta( $post_id, $meta, $value, true );
}
if ( $result ) {
do_action( 'set_post_meta_transient_' . $transient, $post_id, $transient );
do_action( 'setted_post_meta_transient', $transient, $post_id, $transient );
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment