Support Forum
Support › Meta Box Conditional Logic › MetaBox doesn't saves and it doesn't work with selected category
Hi, what I would like to achieve is to have a custom metabox BOX 'activated' when I select a category and another one for a different category.
Here is my code: https://pastebin.com/2X1Suwed
Actually I tryed to use the code generated by the plugin and to copy/paste it in functions.php and it doesn't saves values either ways neither it works.
Could you please tell me how to fix it?
Thank you.
This code works but it is only half of what I would like to achieve:
https://pastebin.com/nWJtSLqz
Hi,
There are several things in this case:
The value for the category select actually is the category ID, so we have to use IDs instead of category slug or name.
The category select has a field type checkbox_tree
, which means its value is an array. We can't use operator =
in this case. Instead, use contains
.
I have adapted your code and made a fix here (please change the categories' IDs to match your case):
Here is my test:
Thank you very much, now, how can I retrieve the data?
I've slightly modified your code to add a prefix: et2018-
https://pastebin.com/QruNZEzY
Let's say I would like to find the value in: et2018-nome_birra
Actually if I would like to retrieve the data I use:
$passaggio = get_post_meta($post->ID, 'gruppo_birra', true);
$birra = $passaggio['et2018-nome_birra'];
echo '<p>Birra: '.$birra.'</p>';
Obviusly it works, but what if the var $birra is empty or worst, what if
$passaggio['et2018-nome_birra']
is empty?
If I try to use this:
<?php $passaggio = get_post_meta($post->ID, 'gruppo_birra', true);
if (isset ($passaggio['et-2018-nome_birra'])){
$birra = $passaggio['et2018-nome_birra'];
echo '<p>Birra: '.$birra.'</p>';
}
It shows nothing even if the variable is set.
Maybe it is a stupid PHP problem, but I can't solve it.
Thank you.
Ok, it works like this:
<?php $passaggio = get_post_meta($post->ID, 'gruppo_birra', true);
if (isset($passaggio['et2018-nome_birra'])){
$birra = $passaggio['et2018-nome_birra'];
}
if (isset($passaggio['et2018-nome_birrificio'])){
$birrificio = $passaggio['et2018-nome_birrificio'];
};
if (isset($passaggio['et2018-gradazione'])){
$gradazione = $passaggio['et2018-gradazione'];
};
if(isset($passaggio['et2018-stile_birra'])){
$stile = $passaggio['et2018-stile_birra'];
};
if (isset($passaggio['et2018-gusto_prevalente'])){
$macrogusto = $passaggio['et2018-gusto_prevalente'];
};
if (isset($passaggio['et2018-gusto_prevalente'])){
$annate = $passaggio['et2018-annata_birra'];
};
if (isset($passaggio['et2018-gusto_descrittori'])){
$descrittori = $passaggio['et2018-gusto_descrittori'];
};
if (isset($passaggio['et2018-prezzo_birra'])){
$prezzo = $passaggio['et2018-prezzo_birra'];
};
if (isset ($passaggio['et2018-disponibilita'])){
$disponibilita = $passaggio['et2018-disponibilita'];
};
if (isset ($passaggio['et2018-formato_birra'])){
$formato = $passaggio['et2018-formato_birra'];
};
if (isset ($passaggio['et2018-quantita_birra'])){
$quantita = $passaggio['et2018-quantita_birra'];
};
if (isset ($passaggio['et2018-annata_birra'])){
$annata = $passaggio['et2018-annata_birra'];
};
echo '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
echo '<p>Birrificio: '.$birrificio.'</p>';
echo '<p>Gradazione: '.$gradazione.'%</p>';
echo '<p>Stile: '.$stile.'</p>';
echo '<p>Macro descrittore: '.$macrogusto.'</p>';
echo 'Annate disponibili: <ul>';
foreach ($annata as $anno){echo '<li>'.$anno.'</li>';};
echo '</ul>';
echo 'Costo: <ul>';
foreach ($prezzo as $prezzi){echo '<li>'.$prezzi.'</li>';};
echo '</ul>';
?>
I think it's better to check like this:
$birra = isset( $passaggio['et2018-nome_birra'] ) ? $passaggio['et2018-nome_birra'] : '';
So the variable is always available and won't display any warning if you do echo $birra;
.
PS: You can get the group value your way or use the helper function like this:
$passaggio = rwmb_meta( 'gruppo_birra' );
In your code, please make sure the global $post
is available. Otherwise it won't work. You can try this instead:
$passaggio = get_post_meta( get_the_ID(), 'gruppo_birra', true );
Really can't understand why but if I do this:
$birra = isset( $passaggio['et2018-nome_birra'] ) ? $passaggio['et2018-nome_birra'] : '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
it doesn't work.
If I do like this, instead:
if (isset($passaggio['et2018-nome_birra'])){
$birra = $passaggio['et2018-nome_birra'];
}
echo '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
It works.
This code:
$birra = isset( $passaggio['et2018-nome_birra'] ) ? $passaggio['et2018-nome_birra'] : '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
is wrong and can't work.
This code:
if (isset($passaggio['et2018-nome_birra'])){
$birra = $passaggio['et2018-nome_birra'];
}
echo '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
works. But it will show a warning if there is no value for $passaggio['et2018-nome_birra']
. And in case there's no value, it still show the <a>
link.
Please try this instead:
$birra = isset($passaggio['et2018-nome_birra']) ? $passaggio['et2018-nome_birra'] : '';
if ($birra) {
echo '<a href="'.get_the_permalink().'" title="'.get_the_title().'"><p>Birra: '.$birra.'</p></a>';
}