0

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>";
Ishio
  • 755
  • 2
  • 9
  • 29
  • `Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:` https://developer.wordpress.org/reference/functions/get_terms/ – git-e-up Apr 11 '18 at 14:31
  • I have tried that as well. No change – Ishio Apr 11 '18 at 14:32
  • Sorry to hear that. The only other thing I might suggest is including the `register_taxonomy_for_object_type()` right after your `register_taxonomy()` function. https://codex.wordpress.org/Function_Reference/register_taxonomy#Usage – git-e-up Apr 11 '18 at 14:53
  • I'm having this issue as well. Please post if you have found a solution. I will post back if I do as well. – mike.bronner Apr 15 '18 at 03:05

1 Answers1

0

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();
mike.bronner
  • 1,203
  • 1
  • 20
  • 39
  • See, I can run through this, but it completely nullifies the `get_terms('custom_tax')` function :\ – Ishio Apr 16 '18 at 16:52
  • I completely agree! I haven't been able to get it to work though, even went through the get_terms function code. – mike.bronner Apr 17 '18 at 00:07