Custom column on CPT not showing correct value when meta data not set

Basically I have 2 custom columns in the 'edit.php' of my custom post type. What I need is when $item_type is set to 'page' but there is no page selected, 'No selection' is echoed (same for when 'media' is set but there is no media file selected). What happens is it's showing the title of the post instead of the proper echo. It works fine for when the page or media file variable is set. Just when it's not set. My code is below, thank you in advance to any help offered.

function my_show_columns($name) {
global $post;
$hidden_page = get_the_title(get_post_meta($post-ID, 'hidden_page', true));

$hidden_file =  get_the_title(get_post_meta($post-ID, 'media_file', true));

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

    switch ($name) {
        case 'hidden_item_type':
            if (empty(get_post_meta($post-ID, 'item_type', true))) {
                echo 'Please select type of item to hide';
            } else {
                echo $item_type;
            }
            break;
        case 'hidden_item_info':
            if(get_post_meta($post-ID, 'item_type', true) == 'page') {
                echo 'Title: ' . $hidden_page;
            } elseif(get_post_meta($post-ID, 'item_type', true) == 'media') {
                echo 'Title: ' . $hidden_file;
            } elseif ( empty(get_post_meta($post-ID, 'hidden_page', true)) ) {
                echo 'No selection';
            } elseif ( empty(get_post_meta($post-ID, 'media_file', true)) ) {
                echo 'No selection';
            }
            break;
    }
}

I'm hooking on at:

        add_action('manage_posts_custom_column',  array($this, 'my_show_columns'));

Topic columns post-meta variables Wordpress

Category Web


Hey so I was able to figure out something that worked. I don't know if this is the best way but it worked for me. Please post an answer if you have a better idea.

Basically I had to change the order in which the statements were in. The final code looked like this:

function my_show_columns($name) {
global $post;
$hidden_page = get_the_title(get_post_meta($post->ID, 'hidden_page', true));

$hidden_file =  get_the_title(get_post_meta($post->ID, 'media_file', true));

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

    switch ($name) {
        case 'hidden_item_type':
            if (empty(get_post_meta($post->ID, 'item_type', true))) {
                echo 'Please select type of item to hide';
            } else {
                echo $item_type;
            }
            break;
        case 'hidden_item_info':
            if ( (get_post_meta($post->ID, 'item_type', true) == 'page') && (empty(get_post_meta($post->ID, 'hidden_page', true))) ) {
                echo 'No selection';
            } elseif ( (get_post_meta($post->ID, 'item_type', true) == 'media') && (empty(get_post_meta($post->ID, 'media_file', true))) ) {
                echo 'No selection';
            } elseif (get_post_meta($post->ID, 'item_type', true) == 'page') {
                echo 'Title: ' . $hidden_page;
            } elseif(get_post_meta($post->ID, 'item_type', true) == 'media') {
                echo 'Title: ' . $hidden_file;
            }
            break;
    }
}

I have no clue as to why this worked and the other way didn't. I'm guessing it's the order in which they're evaluated. Either way, here it is.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.