Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] post meta transient
<?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;
}
@nadiamode

This comment has been minimized.

Copy link

@nadiamode nadiamode commented Jun 8, 2013

nice function apply to post_meta, the wordpress's default transient api store data in option table sometime I don't want store post_meta or post related things in option table and sometime i dont want many roll in option table i want to keep only global data store in option table. this function look like default transient api - just change where to store data now i can use transient features but store it in post_meta table .. this work great if use external object cache plugin like w3 total cache to store data on virtual ram (xcache) or localhost so it result in 0 database query but actually if you already use external object cache plugin you no need to write your own function to store datat in post_meta table instead of option table because plugin will manage your wordpress's default transient api and store it in virtual ram (xcache) or localhost (no option table)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.