{"id":6118326,"date":"2015-05-11T19:19:51","date_gmt":"2015-05-11T19:19:51","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/adding-a-counter-to-add_filter\/"},"modified":"2016-08-24T16:07:56","modified_gmt":"2016-08-24T16:07:56","slug":"adding-a-counter-to-add_filter","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/adding-a-counter-to-add_filter\/","title":{"rendered":"Adding a counter to add_filter ( Plugin: ACF )"},"content":{"rendered":"<p>Hello all<\/p>\n<p>I&#8217;m using add_filter to attempt to add unique IDs to objects. In this case, to Advanced custom fields. Im using a counter with I want to increase with each time the filter function is called. Although my issue is ACF specific, what I want to understand is how variables are passed and returned from a filter.<\/p>\n<p>So, I started by setting up a counter variable in my functions.php :-<\/p>\n<p><code>$counter = 0;<\/code><\/p>\n<p>And then the filter looks like this:-<\/p>\n<pre><code>function my_acf_load_field( $field )\n  {\n  global $counter;\n  $field[&#039;wrapper&#039;][&#039;id&#039;] = &#039;unique-id-&#039;.$counter;\n  $counter++;\n  return $field;\n}\nadd_filter(&#039;acf\/load_field&#039;, &#039;my_acf_load_field&#039;);<\/code><\/pre>\n<p>The trouble is, the counter never increments. It stays at zero. I&#8217;m guessing incrementing the counter inside the filter function doesn&#8217;t then pass the result back, so the counter never goes up.<\/p>\n<p>So, how would I go about putting a counter variable inside a filter, then modifying that counter so it goes up with each call of the function.<\/p>\n<p>I really hope the above makes some sense! Im pretty new to this so I might be getting my terminology wrong. Any help would be appreciated.<\/p>\n","protected":false},"template":"","class_list":["post-6118326","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/6118326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/6118326\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=6118326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}