是的,你可以。
您需要添加一个自定义字段来存储其他帖子类型的ID。我建议使用选择列表。
您可以尝试以下方法:
add_action( \'add_meta_boxes\', \'wpse_143600_add_box\' );
add_action( \'save_post\', \'143600_save_box\' );
//create the metabox
function wpse_143600_add_box() {
add_meta_box(
\'related_testimonial\',
__( \'Testimonails\', \'wpse_143600_translation\' ),
\'wpse_143600_testimonial_box\',
\'locations\',
\'normal\'
);
}
//build the box
function wpse_143600_testimonial_box($post) {
wp_nonce_field( basename( __FILE__ ), \'wpse_143600_nonce\' );
$wpse_143600_stored_meta = get_post_meta( $post->ID );
$testimonialArgs = array(
\'post_type\' => \'testimonials\',
\'post_status\' => \'publish\',
\'numberposts\' => -1
);
$testimonials = get_posts($testimonialArgs);
if($testimonials): ?>
<p>
<label for="meta-select" class="wpse_143600-row-title"><?php _e( \'Example Select Input\', \'wpse_143600_translation\' )?></label>
<select name="meta-select" id="meta-select">
<option value="NULL">Please choose a testimonial…</option>
<?php foreach($testimonials as $testimonial): ?>
<option value="<?php echo $testimonial->ID; ?>" <?php if ( isset ( $wpse_143600_stored_meta[\'meta-select\'] ) ) selected( $wpse_143600_stored_meta[\'meta-select\'][0], $testimonial->ID ); ?>><?php echo $testimonial->post_title; ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
else:
?>
<p>There are no testimonials - please save this post, and write some testimonials. You\'ll then be able to choose a testimonial for this location.</p>
<?php
endif;
}
//save the box
function wpse_143600_save_box( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ \'wpse_143600_nonce\' ] ) && wp_verify_nonce( $_POST[ \'wpse_143600_nonce\' ], basename( __FILE__ ) ) ) ? \'true\' : \'false\';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and saves if needed
if( isset( $_POST[ \'meta-select\' ] ) ) {
update_post_meta( $post_id, \'meta-select\', $_POST[ \'meta-select\' ] );
}
}
这是相当粗糙的代码,但应该可以让您接近。
对不起,长度太长了,您可以在pastebin上找到它:http://pastebin.com/zA4aDiV9