2

This is the filter that i use. add_filter('get_post_metadata', array(WPKWP::CLASS_NAME, 'get_post_metadata'), 10, 4);

I have to use the get_metadata inside the above call back function. If I use directly it will create an infinite loop, so I believe I have to remove the filter temporally and enable it back .

But remove_filter('get_post_metadata', array(WPKWP::CLASS_NAME, 'get_post_metadata'), 0); does not seems to work

 public static function get_post_metadata($meta_value, $post_id, $meta_key, $single){
        if (is_admin()){
            return null;
        }

        remove_filter('get_post_metadata', array(WPKWP::CLASS_NAME, 'get_post_metadata'), 0);
        $child_meta = get_metadata('post', $post_id, $meta_key, false);
        $meta=null;
        if (WPKWP::get_parent()){
            $parent_meta = get_metadata('post', WPKWP::get_parent(), $meta_key, false);

            if (is_array($parent_meta) && is_array($child_meta)) {
                $meta = array_merge($parent_meta, $child_meta);
            } else {
                $meta = $parent_meta;
            }
        } else {
            $meta = $child_meta;
        }
        add_filter('get_post_metadata', array(WPKWP::CLASS_NAME, 'get_post_metadata'), 0, 4);
        return $meta;
    }

Update : @kero add_filter('get_post_metadata', array(WPKWP::CLASS_NAME, 'get_post_metadata'), 0, 4); which is added after removing seems to bring back the filter again. filter mode becomes active even if removed ..any fixes for that ?

  • Does this answer your question? How to hook a filter to catch get_post_meta when alternate a custom field output? – kero Dec 10 '19 at 11:01
  • Sorry, vote-close might have been a bit too quick. First of all: You have another question open - could you delete one of them or are they different? Then: Have you tried it, if the code works in a functional environment, is it really because you're using a class? – kero Dec 10 '19 at 11:03
  • @kero oh.Not sure what's happening, my question is totally different from what's is shown. Also, my previous question is different and I have deleted that. It's inside the class otherwise I would have done it my self – Latheesh V M Villa Dec 10 '19 at 11:10
  • @kero also I used the has_filter(), It returns true even after removing it ..so I believe that it does not work – Latheesh V M Villa Dec 10 '19 at 11:13
  • Can you show your initial add_filter() call? – kero Dec 10 '19 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.