How can I filter the post of a CPT by taxonomy Term in admin meta-box dropdown?

This question is WAY more complex than you realize.

Despite that (or really, because of it) I’m going to give it a go…

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="https://wordpress.stackexchange.com/questions/220336/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']);
    }

}

Note it is not the policy of this network to encourage others to “do your work for you” as I have basically done here – I was just looking for something interesting to do, and the question is asked well. Good luck I hope it works for you! (No guarantee written from scratch like this.)

tech