MetaBox doesn't saves and it doesn't work with selected category
Support › MB Conditional Logic › MetaBox doesn't saves and it doesn't work with selected category
- This topic has 6 replies, 2 voices, and was last updated 7 years ago by
Anh Tran.
-
AuthorPosts
-
April 23, 2018 at 5:53 AM #9309
downloadtaky
ParticipantHi, 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/2X1SuwedActually 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/nWJtSLqzApril 23, 2018 at 9:07 AM #9310Anh Tran
KeymasterHi,
There are several things in this case:
- I see you create 2 groups: 1 group for categories A & B, 1 group for categories C & D. And you put the conditions in each fields of the groups. That's not good for performance because the plugin has to process all fields. Why don't we put the conditions for the groups only? So we need only 2 conditions instead of 10?
-
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, usecontains
.
I have adapted your code and made a fix here (please change the categories' IDs to match your case):
Here is my test:
April 25, 2018 at 6:59 AM #9362downloadtaky
ParticipantThank 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.April 25, 2018 at 8:28 AM #9364downloadtaky
ParticipantOk, 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>'; ?>
April 25, 2018 at 10:26 AM #9366Anh Tran
KeymasterI 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 );
April 26, 2018 at 12:02 AM #9375downloadtaky
ParticipantReally 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.
April 26, 2018 at 2:35 PM #9380Anh Tran
KeymasterThis 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>'; }
-
AuthorPosts
- The topic ‘MetaBox doesn't saves and it doesn't work with selected category’ is closed to new replies.