I have a website which imports videos automatically from Youtube. and I have used these codes in (single.php because it's not working in functions.php) to check if the video has been deleted then redirect the post to its category page
The post like this
<iframe width="560" height="315" src="https://www.youtube.com/embed/Videocode" frameborder="0"></iframe>
The Code
//Check If Video Is Deleted
global $post;
global $wpdb;
$categories = get_the_category($post->ID);
$cat_link = get_category_link($categories[0]->cat_ID);
the_post(); // necessary to use get_the_content()
$links = [get_the_content(get_the_ID())];
foreach($links as $link)
preg_match("#([\/|\?|&]vi?[\/|=]|youtu\.be\/|embed\/)([a-zA-Z0-9_-]+)#", $link, $matches);
//var_dump(end($matches));
$key = (end($matches));
$headers = get_headers('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $key);
if(is_array($headers) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$headers[0]) : false){
// video exists
//echo 'found ID';
} else {
// video does not exist
// Try to mark the post by meta value or tag but not work
// update_post_meta(get_the_ID(),'deletedxxx','yes');
//wp_set_post_terms( $post_id, 'deleted', $taxonomy );
wp_redirect($cat_link,301); exit;
}
I'm trying to mark the post by meta value or tag but not working!! I need two things
1-Mark the post by meta value because
A- (if it has this value stop check again on the video status).
B- (if it has this value redirect it to any post has any of its tags).
2-Remove this post from any loop at the theme
Thanks