Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have taxonomies like Cities and they have child sub taxonomies. I have a code that display as metabox on post publish page. But I want to display on front page as search filter. How can I do that? Thanks in advance.

functions.php

add_action('add_meta_boxes', 'my_custom_metabox'); 
function my_custom_metabox() {
    add_meta_box('custom-taxonomy-dropdown','Brands','taxonomy_dropdowns_box','post','side','high'); } function taxonomy_dropdowns_box( $post ) {
    global $brand_taxonomy, $taxonomy_name;
    wp_nonce_field('custom-dropdown', 'dropdown-nonce');
    $terms = get_terms( $brand_taxonomy, 'hide_empty=0');
    if ( is_a( $terms, 'WP_Error' ) ) {
        $terms = array();
    }

    $object_terms = wp_get_object_terms( $post->ID, $brand_taxonomy, array('fields'=>'ids'));
    if ( is_a( $object_terms, 'WP_Error' ) ) {
        $object_terms = array();
    } ?>
    <script type="text/javascript">
    jQuery(document).ready(function() {
            jQuery('#custombrandoptions').change(function() {
                var custombrand = jQuery('#custombrandoptions').val();
                if ( custombrand == '0') {
                    jQuery('#custommodeloptions').html('');
                    jQuery('#modelcontainer').css('display', 'none');
                } else {
                    jQuery('#ctd-custom-taxonomy-terms-loading').css('display', 'inline');
                    jQuery('#modelcontainer').css('display', 'none');
                    var data = {
                        'action':'get_brand_models',
                        'custombrand':custombrand,
                        'dropdown-nonce': jQuery('#dropdown-nonce').val()
                    };
                    jQuery.post(ajaxurl, data, function(response){
                        jQuery('#custommodeloptions').html(response);
                        jQuery('#ctd-custom-taxonomy-terms-loading').css('display', 'none');
                        jQuery('#modelcontainer').css('display', 'inline');
                    });
                }
            });
    });
</script>
    <?php
    echo "Brand:";
    echo "<select id='custombrandoptions' name='custombrands[]'>";
    echo "<option value='0'>None</option>";
    foreach ( $terms as $term ) {
        if ( $term->parent == 0) {
            if ( in_array($term->term_id, $object_terms) ) {
                $parent_id = $term->term_id;
                echo "<option value='{$term->term_id}' selected='selected'>{$term->name}</option>";
            } else {
                echo "<option value='{$term->term_id}'>{$term->name}</option>";
            }
        }
    }
    echo "</select><br />";
    echo "<div id='ctd-custom-taxonomy-terms-loading' style='display:none;'>Loading...</div>";
    echo "<div id='modelcontainer'";
    if ( !isset( $parent_id)) echo " style='display: none;'";
    echo ">";
    echo "Models:";
    echo "<select id='custommodeloptions' name='custombrands[]'>";
    if ( isset( $parent_id)) {
        $models = get_terms( $brand_taxonomy, 'hide_empty=0&child_of='.$parent_id);
        foreach ( $models as $model ) {
             if ( in_array($model->term_id, $object_terms) ) {
                echo "<option value='{$model->term_id}' selected='selected'>{$model->name}</option>";
            } else {
                echo "<option value='{$model->term_id}'>{$model->name}</option>";
            }
        }
    }
    echo "</select>";
    echo "</div>";
}

add_action('save_post','save_my_custom_taxonomy');
function save_my_custom_taxonomy( $post_id ) {
    global $brand_taxonomy, $taxonomy_name;
    if ( define('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;

    if ( !wp_verify_nonce($_POST['dropdown-nonce'], 'custom-dropdown'))
        return;

    $brands = array_map('intval', $_POST['custombrands']);
    wp_set_object_terms($post_id, $brands, $brand_taxonomy);
} 

add_action('wp_ajax_get_brand_models', 'get_brand_models');
function get_brand_models() {
    global $brand_taxonomy, $taxonomy_name;
    check_ajax_referer('custom-dropdown', 'dropdown-nonce');
    if (isset($_POST['custombrand'])) {
        $models = get_terms( $brand_taxonomy, 'hide_empty=0&child_of='. $_POST['custombrand']);
        echo "<option value='0'>Select one</option>";
        foreach ($models as $model) {
            echo "<option value='{$model->term_id}'>{$model->name}</option>";
        }
    }
    die();
}

source: https://gist.github.com/hameedullah/997350

share|improve this question
1  
For your next question, please read How to Ask. The better formatted and written your question is, the better the answers will be. About your code: Where have you copied/pasted that from? Could you file an edit and add the source? (Hint: Under which license was above code published?) –  kaiser Mar 7 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.