0

I've searched frantically trying to find a solution for this. I'm trying to generate two drop-down fields within one side, Meta Box.

The first drop-down field should be populated by the terms of a chosen taxonomy.

The second should be populated by the posts associated with the the selected taxonomy term based on that selection.

For example,

Say I have the taxonomy "property type" and the terms "flat" or "house" and 3 posts are associated with "houses" and 7 with "flat". How could I get a dual drop down list where one selects "flat" (or House) and from that the associated posts appear in the next dropdown list?

  • Anyone? There must be a way... – NickC Mar 11 '16 at 11:39
  • there's always a way. – majick Mar 12 '16 at 6:43
0

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='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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.