Apply a filter only once

There is no native WordPress hook called add_filter_once(). This is an custom solution which will help you to run your hook only once.

For example if inside a loop or whatever situation you are facing. However,
The basic idea is to check when you need to stop using your hook and simply remove_hook from WordPress and once all done, you need to registere it again.

example code:

function add_filter_once( $hook, $callback, $priority = 10, $args = 1 ) {
    $singular = function () use ( $hook, $callback, $priority, $args, &$singular ) {
        call_user_func_array( $callback, func_get_args() );
        remove_filter( $hook, $singular, $priority );
    };

    return add_filter( $hook, $singular, $priority, $args );
}

The best way to understand what I try to explain to visit this link