0

Okay so I have one metabox for my CPT and it has multiple dropdowns in it. I need help in only allowing one selection to be made.

if ( get_post_meta( $post->ID, 'item_type', true ) == 'page' ) {
        unset( $media_file );
    } elseif ( get_post_meta( $post->ID, 'item_type', true ) == 'media' ) {
        unset( $hidden_page );
    }

This is the code that should do this but when I check in my DB it's not. Here my logic that I need in plain english.... "If item type equals page then get rid of media file selection and if item type equals media then get rid of page selection." Any help offered will be greatly appreciated. Thank you!

EDIT:

Here is the entire meta box class

class meta_boxes {

public $page_dropdown_args = array();
public $media_dropdown_args = array();


/**
 * Constructor.
 */
public function __construct() {
    if ( is_admin() ) {
        add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
        add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );            
    }

    $this->page_dropdown_args = [
        'show_option_none' => '- select a page -',
        'option_none_value' => null,
        'id'               => 'hidden_page',
        'name'             => 'hidden_page',
        'value_field'      => 'hidden_page',
        'disabled' => ''
    ];

    $this->media_dropdown_args = [
        'show_option_none'  => '- select a media file -',
        'post_type'         => 'attachment',
        'name'              => 'media_file',
        'selected'          => '',
        'echo'              => true
    ];

}

/**
 * Meta box initialization.
 */
public function init_metabox() {
    add_action( 'add_meta_boxes', array( $this, 'add_metabox'  ) );
    add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}

public function register() {
    add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}

/**
 * Adds the meta box.
 */
public function add_metabox() {

     add_meta_box(
        'hidden_item',
        'Choose Your Item to Hide',
        array( $this, 'render_meta' ),
        'hidden_items',
        'advanced',
        'default'
        );
}

/**
 * Renders the meta box.
 */


    public function render_meta( $post ) { wp_nonce_field( 'update_post', 'funnel_nonce' );
    $item_type = get_post_meta( $post->ID, 'item_type', true );
    $media_file_link = get_permalink( get_post_meta( $post->ID, 'media_file', true ) );

    ?>
    <div class="wrap">
        <h4 style="display: inline-block;">Type of Item to Hide:</h4>
        <select name="item_type" id="item_type" reuired>
            <option selected disabled value> - Select Type of Item - </option>
            <option value="page" <?php selected( $item_type, 'page' ); ?>>Page</option>
            <option value="media" <?php selected( $item_type, 'media' ); ?>>Media File</option>
        </select>
        <hr style="color: black;">
        <div class="page" id="page">
            <h4 style="display: inline-block;">Select a page:</h4>
            <?php
            $page_args = $this->page_dropdown_args; 
            $page_args['selected'] = get_post_meta( $post->ID, 'hidden_page', true );
            wp_dropdown_pages($page_args);
            ?>
        </div>

        <div class="media" id="media">
            <?php

                $media_args = $this->media_dropdown_args;
                $posts = get_posts(
                    array(
                        'post_type'  => 'attachment',
                        'numberposts' => -1
                    )
                );

                $dropdown = '';

                if( $posts ){

                    if( !is_string($media_args['name']) ){

                        $media_args['name'] = $media_args['attachment'].'_select';
                    }

                    echo '<h4 style="display: inline-block;">Select a media file:&nbsp;</h4>';

                    $dropdown .= '<select id="'.$media_args['name'].'" name="'.$media_args['name'].'">';

                        $dropdown .= '<option selected disabled value="">'.$media_args['show_option_none'].'</option>';

                        $media_args['selected'] = get_post_meta($post->ID, 'media_file', true);

                        foreach( $posts as $p ){

                            $selected = '';
                            if( $p->ID == $media_args['selected'] ){

                                $selected = ' selected';
                            }

                            $dropdown .= '<option value="' . $p->ID . '"'.$selected.'>' . esc_html( $p->post_title ) . '</option>';
                        }

                    $dropdown .= '</select>';           
                }

                if($media_args['name'] === false){

                    return $dropdown;
                }
                else{

                    echo $dropdown;
                }
            
            ?>
        </div>
        
    </div>
    <?

    if ( get_post_meta( $post->ID, 'item_type', true ) == 'page' ) {
        unset( $media_file );
    } elseif ( get_post_meta( $post->ID, 'item_type', true ) == 'media' ) {
        unset( $hidden_page );
    }
}

/**
 * Handles saving the meta box.
 *
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 * @return null
 */
public function save_metabox( $post_id, $post ) {

    // Add nonce for security and authentication.
    $nonce_name   = isset( $_POST['funnel_nonce'] ) ? $_POST['funnel_nonce'] : '';
    $nonce_action = 'update_post';


    // Check if nonce is valid.
    if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) {
        return;
    }

    // Check if user has permissions to save data.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    // Check if not an autosave.
    if ( wp_is_post_autosave( $post_id ) ) {
        return;
    }

    // Check if not a revision.
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    $fields = [

        'item_type' => 'item_type',
        'media_file' => 'media_file',
        'media_file_link' => 'media_file_link',
        'hidden_page' => 'hidden_page'
    ];

    foreach ( $fields as $posted_key => $post_meta_key ) {
        $posted_value = '';

        if ( isset( $_POST[ $posted_key ] ) ) {
            $posted_value = sanitize_text_field( wp_unslash( $_POST[ $posted_key ] ) );
        }

        if ( ! empty( $posted_value ) ) {
            update_post_meta( $post_id, $post_meta_key, $posted_value );
        } 

        else {
            delete_post_meta( $post_id, $posted_key );
        }
    }
}

}

new meta_boxes();

1
  • If you created metabox with your own code, share a code, please. The code you shared tells me that values already saved (and you are trying to unset something), but description suggests, that you need to save just one of these value. – anton May 11 at 19:53

Your Answer

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

Browse other questions tagged or ask your own question.