I'm having trouble making the code roll.
I have the variable $posts_metas
that can be empty or have an array, it depends on what the user has already registered in the DB.
I also have the $uls
containing urls of the images that the user selected.
I'm trying to do some checks before saving to DB, see comments in the code below:
add_action( 'save_post', function ($post_id) {
if (isset($_POST['my_image_URL'])){ //se existem imagens selecionadas
$urls = $_POST['my_image_URL']; // variavel urls recebe as urls das imagens
$posts_metas = get_post_meta( $post_id, 'my-image-for-post' ); // busca no DB posts metas onde a coluna meta_key sejam = "my-image-for-post" e retorna as urls
if ($posts_metas === ''){ //se posts metas retornou vazio
foreach ($urls as $url){
add_post_meta( $post_id, 'my-image-for-post',$url ); // apenas cadastra todas urls... ate aqui quando o DB esta vazio cadastra corretamente as urls
}
}
}else{
foreach ($urls as $url){
foreach( $posts_metas as $value ) {
if($value != $url){
add_post_meta( $post_id, 'my-image-for-post',$url );
}elseif($value === $url ){
update_post_meta( $post_id, 'my-image-for-post', $url );
}else{
add_post_meta( $post_id, 'my-image-for-post',$url );
}
}
}
}
});
No else
if there is already some meta_key for the post with the value 'my-image-for-post' then you have some url registered.
I'm trying to execute the following 2 conditions:
1st if all registered values are different from the new url or if the url does not exist in the DB you should add the url.
If the value of the url is identical to some already registered url, it should only update
It happens that after else
, I'm not able to hit the foreach and loop
Thank you for your help