[WordPress] post_meta に有効期限付きの値を挿入する

post_meta でも、transient API みたいに有効期限付きに出来たら良いのにと思っていたあなたに


<?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;
}


Posted

in

by

Comments

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。