static variable loop not working in WordPress

I have a function in my wordpress theme that deletes the first embedded video in a video post. See code below. This is in the functions.php file.

/* - function that hides first video in post content - */

function process_embed( $embed ){

    if ( is_single() && get_post_format() === 'video' ) {
        static $post_video_num = 0;


        // Hide first video in the post content on single video post page
        if ( 1 === $post_video_num ) {
            return '';

    return $embed;

add_filter( 'embed_oembed_html', 'process_embed', 10, 3 );
add_filter( 'video_embed_html', 'process_embed' ); // Jetpack

As you can see, if the post is a single post and it’s a video format, it will declare a static variable and iterate it everytime a video is in the post. If the static variable $post_video_num is 1 (meaning the first video in the post) it is replaced with blank, removing the first embed video.

This code works fine in my development environment on my local machine. However, it doesn’t seem to work on my live server. That is the problem.

While I was debugging, after echoing out the $post_video_num variable, I found out that it won’t remove the first video because the variable $post_video_num is 8 (instead of 1, as it should be).

After echoing out the $post_video_num numbers, what is happening is that it echoes out the numbers 1-8 on top of the page, then echoes out 8-12 in place of where the first video should be. Specifically on the live server, the function seems to loop multiple times, which is why it doesn’t work.

The core problem, which is what is puzzling me, is that this function works as intended on my local machine but not on the live server, as it does this strange looping thing which I can’t explain.

What would cause this function to work on my local machine and not the live server? Is there something I am missing here?

Thanks! I hope this makes sense. You’re help is greatly appreciated.

Solutions Collecting From Web of "static variable loop not working in WordPress"