post_meta でも、transient API みたいに有効期限付きに出来たら良いのにと思っていたあなたに
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
<?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; | |
} |
コメントを残す