yuheijotaki.com

2018/04/25 : 
update_field(); カスタムフィールドに動的に値をを入れ込む

予めACFなどで ‘post_views’ フィールド(数値フィールド)を作成しておく。

// ----------------------------------------
// 閲覧数用フィールド
// singleページにアクセスしたとき、その投稿の'post_views'フィールドにプラス1する
// ----------------------------------------
function update_custom_meta_views() {
	global $post;
	if ( is_single() ) {
		$id = get_the_ID();
		$post_views = get_field('post_views', $id);
		$post_views++;
		update_field('post_views', $post_views, $id);
	}
}
add_action( 'wp_head', 'update_custom_meta_views' );