When you submit the form there is a new request to the server. Because global $loop_anwaelte;
is populated on add_meta_box
it will not be populated on save_post
which fires much, much earlier in the new page load. I am pretty sure that is why your “save” isn’t working. The foreach ( $loop_anwaelte as $anwalt )
simply does nothing, and if you had debugging enabled you’d likely see errors/warnings.
There are several ways to solve this:
- Populate the array twice, once on save and once on the meta box
display - Populate the
global
early in the page load, for example, on one of
theload-{page_hook}
hooks - Better, use option #2 and convert the whole code base into a class
to avoid theglobal
altogether. - Rewrite your form so that you are passing an array through
$_POST
that you can then iterate over.
Option 1) is inefficient for obvious reasons.
Options 2) and 3) are prefered, with 3) being much, much neater.
Option 4) would have validation concerns to deal with as $_POST
data– all $_POST
data including the key names– is subject to user manipulation.