1

I am currently using a custom post type, with a custom field that is a checkbox. There are multiple values in the checkbox including: Laptop, New, No-Marketing-text and several more. If No-marketing-text is the only one selected, it has no problem with doing what I want it to do. If multiple checkboxes are selected, it only looks at the first one that is selected. What should I change to make it work even if multiple checkboxes are selected?

<?php

      //REMOVES BACK TO TOP BUTTON FOR PROGRAMS THAT HAVE NO MARKETING BLURB - JUNE 27, 2013 - Brandon Carson

    if("No-marketing-text" == get_post_meta($post->ID, 'program_flags', true)){

            echo "";

        }else{
            echo "<div class='content-block'>";

            the_content();

        echo "<a class='back-to-top-link' href='#top'>Back to Top</a></div>";


        }

    ?>
1

Under the premise, that your database entries do get saved correctly and your problem occurs on retrieval only:

If multiple boxes had been selected, the values should live in the database as serialized arrays.
Fortunately, get_post_meta unserializes them for us, but they are still arrays.
Hence, your conditional should be modified like so:

$program_flags = get_post_meta( $post->ID, 'program_flags', true );

if(
    'No-marketing-text' === $program_flags
    ||
    ( is_array( $program_flags ) && in_array( 'No-marketing-text', $program_flags ) )
){
    // do your thing
}
4
  • <?php //REMOVES BACK TO TOP BUTTON FOR PROGRAMS THAT HAVE NO MARKETING BLURB - JUNE 27, 2013 - Brandon Carson $program_flags = get_post_meta( $post->ID, 'program_flags', true ); if('Laptop' === $program_flags || ( is_array( $program_flags ) && in_array( 'Laptop' ))) { echo "I see laptop"; }else{ echo "<div class='content-block'>I do not see laptop"; echo "<a class='back-to-top-link' href='#top'>Back to Top</a></div>"; } ?> I tried this in our development area and it did not work. – Brandon Jul 16 '13 at 16:07
  • in_array( 'Laptop', $program_flags ) --> sorry, forgot second argument. – Johannes Pille Jul 16 '13 at 16:10
  • I also tried adding the additional code that you edited, and it did not work. Still only works if it is the only check box selected. Thank you for your help with this so far. – Brandon Jul 16 '13 at 17:39
  • Any other ideas @johannes-Pille ? – Brandon Jul 16 '13 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.