function __construct()
{
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 );
add_action('save_post', array($this, 'wpt_save_article_meta'), 1, 2); // save the custom fields
}
public function add_meta_boxes() {
add_meta_box( 'game-post', __('Game'), array( $this,'game_post_meta_box'), 'post', 'normal', 'low' );
}
function game_post_meta_box($post){
$games = $this->getPostsByType('game');
$game_id ='';
if ( metadata_exists( 'post', $post->ID, 'game_id' ) ) {
$game_id = get_post_meta( $post->ID, 'game_id', true );
}
if(!empty($games)){
?>
<select name="game_id">
<option value="">Please select game</option>
<?php
foreach ($games as $game){
?>
<option value="<?php echo $game->ID; ?>" <?php if($game_id==$game->ID){ echo 'selected="selected"'; } ?>><?php echo $game->post_title; ?></option>
<?php
}
?>
</select>
<?php
}
}
public function wpt_save_article_meta($post_id, $post){
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if(isset($_POST['game_id'])){
$game_id = $_POST['game_id'];
update_post_meta( $post_id, 'game_id', $game_id);
}
}