require( ABSPATH . WPINC . '/capabilities.php' );
require( ABSPATH . WPINC . '/user.php' );
require( ABSPATH . WPINC . '/formatting.php' );
require( ABSPATH . WPINC . '/meta.php' );
wp_plugin_directory_constants( );
ms_cookie_constants( );
wp_cookie_constants( );
require( ABSPATH . WPINC . '/pluggable.php' );
//リクエストされている画像のURLを取得する
$image_uri = $_SERVER['REQUEST_URI'];
$image_url = 'http://example.jp' . $image_uri;
// そのURLの画像が紐づいているポストのIDを取得する
// postsテーブルからURLでpost_parentを検索
$parent_post_id = $wpdb->get_var($wpdb->prepare( "SELECT post_parent FROM $wpdb->posts WHERE guid = '$image_url';" ));
// そのポストIDを持つ投稿にくっついているタグ(open/closed)を取得する
// term_relationshipsテーブルで、ポストIDにくっついているterm_taxonomy_idを把握する
$parent_tax_id = $wpdb->get_col($wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$parent_post_id';" ));
// そのterm_taxonomy_idが2なら公開、3なら非公開なのです。
// in_arrayで3が含まれる→TRUE(つまり非公開)
$open_flag = in_array('3', $parent_tax_id);
// 非ログインかつ非公開タグが付いている場合に404
if ( !is_user_logged_in() && $open_flag ) {
status_header(404);
die('No images displayed when the owner sets closed');
}