这个问题比你想象的要复杂得多。
尽管如此(或者真的,因为它),我还是要尝试一下。。。
add_metabox(\'terms-post-selector\',\'Select Related Post\',\'post_terms_selector_dropdown\',null,\'post\',\'side\',\'high\');
function post_terms_selector_dropdown() {
global $post;
$taxonomy = \'property_type\'; // or whatever it is
$terms = get_the_terms($postid,$taxonomy);
$selectedterm = get_post_meta($post->ID,\'_selected_term\',true);
if (count($terms) > 0) {
echo "<select name=\'termselector\' id=\'termselector\' onchange=\'loadpostterms();\'>";
foreach ($terms as $term) {
echo "<option value=\'".$term->value."\'";
if ($term->value == $selectedterm) {echo " selected=\'selected\'";}
echo ">".$term->name."</option>";
}
echo "</select>";
echo "<div id=\'posttermselecter\'></div>";
}
// javascript to trigger the AJAX creation of post dropdown select element
echo "<script>function loadpostterms() {
var ajaxurl = \'".admin_url(\'admin-ajax.php\')."\';
var termselector = document.getElementById(\'termselector\');
var selectedterm = termsselector.options[termselector.selectedIndex].value;
document.getElementByID(\'termqueryiframe\').src = ajaxurl+\'?action=post_terms_dropdown&term=\'+selectedterm+\'&postid=".$post->ID."\';
</script>";
// just a dummy iframe to handle the AJAX post selector callback
echo "<iframe src=\'javascript:void(0);\' id=\'termqueryiframe\' style=\'display:none;\'></iframe>";
}
// AJAX callback to populate the post select element
add_action(\'wp_ajax_post_terms_dropdown\',\'post_terms_dropdown\');
add_action(\'wp_ajax_nopriv_post_terms_dropdown\',\'post_terms_dropdown\');
function post_terms_dropdown() {
// get all posts with specified term
$term = $_GET[\'term\'];
$taxonomy = \'property_type\'; // or whatever it is
$args = array(\'tax_query\' => array(array(
\'taxonomy\' => $taxonomy, \'field\' => \'ID\', \'terms\' => $term
) ) );
$posts = get_posts($args);
if (count($posts) > 0) {
// get the currently selected post
$postid = $_GET{\'postid\'];
$selectedpost = get_post_meta($postid,\'_selected_post\',true);
// create the post dropdown and send it back to parent frame
echo "<script>var postselector = \'<select name=\\"posttermselector\\">\';";
foreach ($posts as $post) {
echo "postselector += \'<option value=\\"".$post->ID."\\"";
if ($post->ID == $selectedpost) {echo " selected=\\"selected\\"";}
echo ">".$vpost->post_title."</option>\';";
}
echo "postselector += \'</select>\';
parent.document.getElementById(\'posttermselector\').innerHTML = postselector;</script>";
}
exit;
}
// update the hidden post fields on post publish / save
add_action(\'publish_post\',\'select_post_terms_update\');
add_action(\'save_post\',\'select_post_terms_update\');
function select_post_terms_update($postid) {
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {return;}
if (!current_user_can(\'edit_post\',$postid)) {return;}
if (isset($_POST[\'termselector\'])) {
update_post_meta($postid,\'_selected_term\',$_POST[\'termselector\']);
}
if (isset($_POST[\'posttermselector\'])) {
update_post_meta($postid,\'_selected_post\',$_POST[\'posttermselector\']);
}
}
请注意,这个网络的政策并不是像我在这里所做的那样鼓励他人“为你做你的工作”——我只是想做一些有趣的事情,这个问题问得很好。祝你好运,我希望它对你有用!(没有像这样从头开始写的担保。)