So I figured out a work around by butchering this and placing in parts from other solutions found online. This is the code I got working correctly.
functions.php:
/* ** START ** ADDS A NEW META BOX TO PAGES */
// register the meta box
add_action( 'add_meta_boxes', 'my_custom_field_cityselect' );
add_action( 'load-page.php', 'my_custom_field_cityselect' );
add_action( 'load-page-new.php', 'my_custom_field_cityselect' );
function my_custom_field_cityselect() {
add_meta_box(
'my_select_meta_box_id', // this is HTML id of the box on edit screen
'Select The City For This Page', // title of the box
'my_customfield_box_content', // function to be called to display the select, see the function below
'page', // on which edit screen the box should appear
'normal', // part of page where the box should appear
'high' // priority of the box
);
}
// display the metabox
function my_customfield_box_content($post) {
// nonce field for security check, you can have the same
// nonce field for all your meta boxes of same plugin
global $post;
// storing the global post object so it doesn't get mixed up with the options
$post_old = $post;
$custom = get_post_custom($post->ID);
if (isset($custom["meta_element_class"][0])) {
$meta_element_class = $custom["meta_element_class"][0];
} else {
$meta_element_class="0";
}
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'page-home-template.php'
)
)
);
$posts = get_posts($args);
echo '<select style=\'width:300px;\' name=\'my_metabox_location\' id=\'my_metabox_location\'>';
echo '<option style=\'color:red\' value=\'0\''. selected( $meta_element_class, '0' ) .'>Choose Appropriate Location</option>';
foreach( $posts as $post ) : setup_postdata($post);
$mymbpostid = $post->ID;
$mymbposttitle = str_replace("Weight Loss ","",get_the_title());
$mymbposttitle = str_replace("Home","(HOME PAGE) - DO NOT USE THIS OPTION",get_the_title());
echo '<option style=\'color:green\' value=\''. $mymbpostid .'\''. selected( $meta_element_class, $mymbpostid ) .'>'. $mymbposttitle .'</option>';
endforeach;
echo '</select>';
// restoring the global post object
$post = $post_old;
setup_postdata( $post );
}
// save data from select
add_action( 'save_post', 'my_custom_field_data' );
function my_custom_field_data() {
global $post;
// check if this isn't an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// further checks if you like,
// for example particular user, role or maybe post type in case of custom post types
// now store data in custom fields based on select option
if ( isset( $_POST['my_metabox_location'] ) )
{
update_post_meta( $post->ID, 'meta_element_class', $_POST['my_metabox_location'] );
}
}
// Removes the Meta Box from the Home Page Template
add_action('admin_head','my_meta_init');
function my_meta_init(){
$template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);
if (($template_file == 'page-home-template.php')) {
remove_meta_box('my_select_meta_box_id','page','advanced');
}
}
// Add styles for meta box
add_action('admin_head', 'custom_admin_meta_css');
function custom_admin_meta_css() {
echo '<style>#my_select_meta_box_id h2 {color:red}</style>';
}
/* ** END ** ADDS A NEW META BOX TO PAGES */
And this code in the page.php:
<?php $meta_element_class = get_post_meta($post->ID, 'meta_element_class', true); ?>
<?php $loc_phone = get_field('location_phone_number', $meta_element_class); ?>
<?php echo '<h2>Page ID - '. $meta_element_class .' | Page Phone - '. $loc_phone .'</h2>'; ?>
With WordPress 5.0.3 and the Classic Editor plugin installed I still can’t get the meta box to display above the content editor though. If anyone knows how to do that, that help would be greatly appreciated.