0 ) {
update_post_meta( $post_id, 'ptmf_selected_posts', $selected_post_id );
}
if ( $selected_term_id > 0 ) {
update_post_meta( $post_id, 'ptmf_selected_term', $selected_term_id );
}
return $post_id;
}
add_action( 'save_post', 'ptmf_save_metabox' );
function ptmf_display_metabox($post) {
$selected_post_id = get_post_meta($post->ID,'ptmf_selected_posts',true);
$selected_term_id = get_post_meta($post->ID,'ptmf_selected_term',true);
wp_nonce_field( 'ptmf_posts', 'ptmf_posts_nonce' );
$args = array(
'post_type' => 'post',
'posts_per_page' => - 1
);
$dropdown_list = '';
$_posts = new wp_query( $args );
while ( $_posts->have_posts() ) {
$extra = '';
$_posts->the_post();
if(in_array(get_the_ID(),$selected_post_id)){
$extra = 'selected';
}
$dropdown_list .= sprintf( "%s", $extra, get_the_ID(), get_the_title() );
}
wp_reset_query();
$_terms = get_terms(array(
'taxonomy'=>'genre',
'hide_empty' => false,
));
$term_dropdown_list = '';
foreach($_terms as $_term){
$extra = '';
if($_term->term_id == $selected_term_id){
$extra = 'selected';
}
$term_dropdown_list .= sprintf( "%s", $extra, $_term->term_id, $_term->name );
}
$label = __( 'Select Posts', 'post-tax-metafield' );
$label2 = __( 'Select Term', 'post-tax-metafield' );
$metabox_html = <<
{$label}
{$label}
{$dropdown_list}
{$label2}
{$label2}
{$term_dropdown_list}
EOD;
echo $metabox_html;
}
if ( ! function_exists( 'ptmf_is_secured' ) ) {
function ptmf_is_secured( $nonce_field, $action, $post_id ) {
$nonce = isset( $_POST[ $nonce_field ] ) ? $_POST[ $nonce_field ] : '';
if ( $nonce == '' ) {
return false;
}
if ( ! wp_verify_nonce( $nonce, $action ) ) {
return false;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return false;
}
if ( wp_is_post_autosave( $post_id ) ) {
return false;
}
if ( wp_is_post_revision( $post_id ) ) {
return false;
}
return true;
}
}