// --> In functions.php
// Build Arrays
$artistList = array();
$genresList = array();
add_action('init', 'getArtists');
add_action('init', 'getGenres');
function getArtists()
{
global $artistList;
$loop = new WP_Query( array( 'post_type' => 'artists' ) );
while ( $loop->have_posts() ) : $loop->the_post();
array_push($artistList, get_the_title());
endwhile;
}
function getGenres()
{
global $genresList;
$terms = get_terms('genres', array( 'hide_empty' => 0));
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
array_push($genresList, $term->name);
}
}
}
// --> In custom plugin file
add_action( 'add_meta_boxes', 'tracks_meta_box_add' );
add_action( 'save_post', 'tracks_meta_box_save' );
function tracks_meta_box_add()
{
add_meta_box( 'tracks-artist-mb', 'Artist', 'initTracksArtistMb', 'tracks', 'normal', 'high' );
add_meta_box( 'tracks-genre-mb', 'Genre', 'initTracksGenreMb', 'tracks', 'normal', 'high' );
add_meta_box( 'tracks-position-mb', 'Chart Position', 'initTracksPositionMb', 'tracks', 'normal', 'high' );
add_meta_box( 'tracks-vimeoLink-mb', 'Vimeo Link', 'initTracksVimeoMb', 'tracks', 'normal', 'high' );
add_meta_box( 'tracks-youtubeLink-mb', 'YouTube Link', 'initTracksYoutubeMb', 'tracks', 'normal', 'high' );
}
function initTracksArtistMb($post)
{
global $artistList;
$values = get_post_custom( $post->ID );
$selected = isset( $values['track_artist'] ) ? esc_attr( $values['track_artist'][0] ) : '';
//wp_nonce_field( 'artist_nonce', 'meta_box_nonce' );
echo '<p><em>• Choose an existing artist</em></p>';
echo '<label for="track_artist"></label>';
echo '<select name="track_artist" id="track_artist">';
echo '<option>Select an Artist...</option>';
foreach ($artistList as $artist)
{
?>
<option value="<?php echo $artist; ?>" <?php selected($selected, $artist); ?>><?php echo $artist; ?></option>
<?php
}
echo '</select>';
echo '<p><em>• Or add a new artist</em></p>';
echo '<p><a href="#">Add new Artist</a></p>';
}
function initTracksGenreMb($post)
{
//wp_nonce_field( 'genre_nonce', 'meta_box_nonce' );
global $genresList;
$values = get_post_custom( $post->ID );
$check = isset( $values['track_genres'] ) ? esc_attr( $values['track_genres'] ) : '';
$count = sizeof($genresList);
echo '<p><em>• Choose from existing genres</em></p>';
echo '<div id="genre-check-list" class="sl-node">';
for($i=0; $i<$count; $i++)
{
echo '<div class="genre-check-item sl-node">';
echo '<input type="checkbox" id="track_genres" name="track_genres" ', checked( $check, 'on' ), '/>';
echo '<label for="', $genresList[$i], '">', $genresList[$i], '</label>';
echo '</div>';
}
echo '</div>';
echo '<p><em>• Or add a new genre</em></p>';
echo '<p><a href="#">Add New Genre</a></p>';
}
function tracks_meta_box_save($post_id)
{
// Save the data
}