WordPress Custom Fields (Checkboxes with multiple values)

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


        }

    ?>

Related posts

1 comment

  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
    }
    

Comments are closed.