get_terms() returns Invalid Taxonomy

2019-08-23 12:43发布

I've looked at other examples and solutions, but there has to be something I am missing. I've tried everything the other posts answers have suggested, but to no avail.

I'm not sure what exactly the issue is, but I can use get_terms without any parameters, and I get every term on my site, but clearly I want to isolate this to only the taxonomy declared. I am running this outside a loop, so I cannot pass an ID, nor do I want to pass an ID.

Any insight is helpful!

I am getting this when using get_terms().

object(WP_Error)#992 (2) {
  ["errors"]=>
  array(1) {
    ["invalid_taxonomy"]=>
    array(1) {
      [0]=>
      string(17) "Invalid taxonomy."
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

My Code:

Taxonomy

if ( ! function_exists( 'car_ctax_catalog' ) ) {
  // Register Custom Taxonomy
  function car_ctax_catalog() {

    $labels = array(
      'name'                       => _x( 'Categories', 'Taxonomy General Name', 'carmon' ),
      'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'carmon' ),
      'menu_name'                  => __( 'Category', 'carmon' ),
      'all_items'                  => __( 'All Categories', 'carmon' ),
      'parent_item'                => __( 'Parent Category', 'carmon' ),
      'parent_item_colon'          => __( 'Parent Category:', 'carmon' ),
      'new_item_name'              => __( 'New Category Name', 'carmon' ),
      'add_new_item'               => __( 'Add New Category', 'carmon' ),
      'edit_item'                  => __( 'Edit Category', 'carmon' ),
      'update_item'                => __( 'Update Category', 'carmon' ),
      'view_item'                  => __( 'View Category', 'carmon' ),
      'separate_items_with_commas' => __( 'Separate categories with commas', 'carmon' ),
      'add_or_remove_items'        => __( 'Add or remove categories', 'carmon' ),
      'choose_from_most_used'      => __( 'Choose from the most used', 'carmon' ),
      'popular_items'              => __( 'Popular Categories', 'carmon' ),
      'search_items'               => __( 'Search Categories', 'carmon' ),
      'not_found'                  => __( 'Not Found', 'carmon' ),
      'no_terms'                   => __( 'No categories', 'carmon' ),
      'items_list'                 => __( 'Categories list', 'carmon' ),
      'items_list_navigation'      => __( 'Categories list navigation', 'carmon' ),
    );
    $args = array(
      'labels'                     => $labels,
      'hierarchical'               => true,
      'public'                     => true,
      'show_ui'                    => true,
      'show_admin_column'          => true,
      'show_in_nav_menus'          => true,
      'show_tagcloud'              => false,
      'rewrite'                    => false,
      'show_in_rest'               => true,
      'rest_base'                  => 'ctax_catalog',
    );
    register_taxonomy( 'ctax_catalog', array( 'cpt_catalog' ), $args );

  }
  add_action( 'init', 'car_ctax_catalog', 0 );
}

Get Terms

$args = array (
    'post_type' => 'catalog',
    'taxonomy'  =>  'ctax_catalog',
    'hide_emoty'  =>  false
);
$tax = array ( 'ctax_catalog' );

$terms  = get_terms($tax, $args);
echo"<pre>";var_dump($terms);echo"</pre>";

EDIT

I've also tried this with the same result.

Get Terms Try 2

$terms  = get_terms('ctax_catalog');
$tterms = get_terms( array ('taxonomy' => 'ctax_catalog' ) );
echo"<pre>";var_dump($terms);echo"</pre>";
echo"<pre>";var_dump($tterms);echo"</pre>";

标签: wordpress
1条回答
贪生不怕死
2楼-- · 2019-08-23 13:01

I came up with the following hackish work-around, until a better solution could be found:

$terms = get_terms();
$gradeOptions = "";

foreach ($terms as $term) {
    if ($term->taxonomy === "pa_grade") {
        $gradeOptions .= "<options value=\"{$term->term_id}\">{$term->name}</options>";
    }
}
var_dump($gradeOptions);
die();
查看更多
登录 后发表回答