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

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #9309
    downloadtakydownloadtaky
    Participant

    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

    #9310
    Anh TranAnh Tran
    Keymaster

    Hi,

    There are several things in this case:

    1. 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?
    2. The value for the category select actually is the category ID, so we have to use IDs instead of category slug or name.

    3. 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):

    https://pastebin.com/prL8r3Ra

    Here is my test:

    https://imgur.com/yunSkXX

    #9362
    downloadtakydownloadtaky
    Participant

    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.

    #9364
    downloadtakydownloadtaky
    Participant

    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>';
                ?>
    #9366
    Anh TranAnh Tran
    Keymaster

    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 );

    #9375
    downloadtakydownloadtaky
    Participant

    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.

    #9380
    Anh TranAnh Tran
    Keymaster

    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>';
    }
Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘MetaBox doesn't saves and it doesn't work with selected category’ is closed to new replies.