最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get_terms does not return any results for my custom taxonomy?

programmeradmin0浏览0评论

If I go to wp-admin/edit-tags.php?taxonomy=location then I correctly see a list of terms.

If I loop through:

get_terms("category");

Then I correctly see several terms for the taxonomy of category. Have I created my custom taxonomy incorrectly for the same function not to output any results for my taxonomy?

get_terms("location");

register_taxonomy('location', 'post', 
array(      
  'labels' => array( 'name' => _x( 'Locations',
    'taxonomy general name' ), 
    'singular_name' => _x( 'Location', 'taxonomy singular name' ), 
    'search_items' => __( 'Search Locations' ), 
    'all_items' => __( 'All Locations' ), 
    'parent_item' => __( 'Parent Location' ), 
    'parent_item_colon' => __( 'Parent Location:' ), 
    'edit_item' => __( 'Edit Location' ), 
    'update_item' => __( 'Update Location' ), 
    'add_new_item' => __( 'Add New Location' ), 
    'new_item_name' => __( 'New Location Name' ), 
    'menu_name' => __( 'Locations' ), 
  ),     
  'rewrite' => array( 'slug' => 'locations',   
  'with_front' => false,   
  'hierarchical' => true   
  ), 
)
);

If I go to wp-admin/edit-tags.php?taxonomy=location then I correctly see a list of terms.

If I loop through:

get_terms("category");

Then I correctly see several terms for the taxonomy of category. Have I created my custom taxonomy incorrectly for the same function not to output any results for my taxonomy?

get_terms("location");

register_taxonomy('location', 'post', 
array(      
  'labels' => array( 'name' => _x( 'Locations',
    'taxonomy general name' ), 
    'singular_name' => _x( 'Location', 'taxonomy singular name' ), 
    'search_items' => __( 'Search Locations' ), 
    'all_items' => __( 'All Locations' ), 
    'parent_item' => __( 'Parent Location' ), 
    'parent_item_colon' => __( 'Parent Location:' ), 
    'edit_item' => __( 'Edit Location' ), 
    'update_item' => __( 'Update Location' ), 
    'add_new_item' => __( 'Add New Location' ), 
    'new_item_name' => __( 'New Location Name' ), 
    'menu_name' => __( 'Locations' ), 
  ),     
  'rewrite' => array( 'slug' => 'locations',   
  'with_front' => false,   
  'hierarchical' => true   
  ), 
)
);
Share Improve this question asked Jan 19, 2012 at 13:28 KevinUKKevinUK 3412 silver badges13 bronze badges 1
  • Sorry - the reply was in response to the answer above. Got posted in the wrong place. – user25516 Commented Jan 3, 2013 at 10:34
Add a comment  | 

3 Answers 3

Reset to default 11

If you are trying to use all of the terms in that taxonomy for something, try this:

get_terms( "location", array( "hide_empty" => 0 ) );

You may be trying to return taxonomy terms that have no relationship to any object.

Try using WP_Term_Query:

Get all args from here: https://developer.wordpress/reference/classes/WP_Term_Query/__construct/

$term_query = new WP_Term_Query( array( 
    'taxonomy' => 'regions', // <-- Custom Taxonomy name..
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'child_of'               => 0,
    'parent' => 0,
    'fields'                 => 'all',
    'hide_empty'             => false,
    ) );


// Show Array info
echo "<pre>";
print_r($term_query->terms);
echo "</pre>";


//Render html
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name .", ";
echo $term->term_id .", ";
echo $term->slug .", ";
echo "<br>";
}
} else {
echo '‘No term found.’';
}

According to WordPress Reference:

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms( array(
        'taxonomy' => 'post_tag',
        'hide_empty' => false,
) );

Jared's answer helped to point towards this.

发布评论

评论列表(0)

  1. 暂无评论