How to retrieve select_tree taxonomy values

Support General How to retrieve select_tree taxonomy values

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1083
    garydgaryd
    Participant

    I declared a taxonomy metabox field like so:

    // TAXONOMY - Neighborhood
    			array(
    				'name'			=> __( 'Neighborhood', 'meta-box' ),
    				'id'			=> "{$prefix}neighborhood",
    				'type'			=> 'taxonomy',
    				'columns'		=> 4,
                    'tab'			=> 'summary',
    				'options'		=> array(
    					// Taxonomy name
    					'taxonomy'	=> 'neighborhood',
    					// How to show taxonomy: 'checkbox_list' (default) or 'checkbox_tree', 'select_tree', select_advanced or 'select'. Optional
    					'type'		=> 'select_tree',
    					// Additional arguments for get_terms() function. Optional
    					'args'		=> array()
    				),
    ),

    And I am accessing the named vars within it like so:

    $neighborhood = rwmb_meta('sg_neighborhood', 'type=taxonomy&taxonomy=neighborhood');
    
    foreach ($neighborhood as $term) {
    	$desc = $term->description;
    	$hood = $term->name;
    	$prnt = $term->parent;
    }
    echo $hood . ', ' . $prnt;
    

    Which results in:

    http://cl.ly/image/2B0N1i082h3E

    I have my neighborhoods taxonomy set up as parent > child, but when I dump the array they show as siblings. And the result of echo $hood is always the last $term->name
    Results of var_dump($neighborhoods);

    array(2) {
      [0]=>
      object(stdClass)#2507 (10) {
        ["term_id"]=>
        int(1237)
        ["name"]=>
        string(8) "Belltown"
        ["slug"]=>
        string(8) "belltown"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(1347)
        ["taxonomy"]=>
        string(12) "neighborhood"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(1236)
        ["count"]=>
        int(25)
        ["filter"]=>
        string(3) "raw"
      }
      [1]=>
      object(stdClass)#2672 (10) {
        ["term_id"]=>
        int(1236)
        ["name"]=>
        string(16) "Downtown Seattle"
        ["slug"]=>
        string(16) "downtown-seattle"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(1358)
        ["taxonomy"]=>
        string(12) "neighborhood"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(0)
        ["count"]=>
        int(51)
        ["filter"]=>
        string(3) "raw"
      }
    }

    Here is how it looks in the editor:
    Backend editor view

    And the code behind:
    Code behind

    Thanks for any help

    #1086
    Anh TranAnh Tran
    Keymaster

    Hi, currently the rwmb_meta function uses get_terms() internally and this function doesn't return terms in hierarchical order. I think the best way for now is going through the returned items, looking for parent parameter to get the correct order.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘How to retrieve select_tree taxonomy values’ is closed to new replies.