1

How to solve WP_Error Object for get_terms function ? I registered custom taxonomy tax_alone_event at the coustom-post-type.php file. and include at the functions.php file. Here is my code

function get__terms_list() {
  $terms = get_terms('tax_alone_event');
  return $terms ;

} //End get_terms_list()..

print_r(get__terms_list());

And I register custom post and custom taxonomy code at coustom-post-type.php file

function Alone_setup_post_type_project() {
    // event custom post and tax 
    $alone_event_args = array(
        'labels'             => array(
            'name'               => _x( 'Event', 'post type general name', 'alonefoundation' ),
             'add_new'            => _x( 'Add New', 'Event', 'alonefoundation' ),
             'add_new_item'       => __( 'Add New Event', 'alonefoundation' ),
             'new_item'           => __( 'New Event', 'alonefoundation' ),
             'edit_item'          => __( 'Edit Event', 'alonefoundation' ),
             'view_item'          => __( 'View Event', 'alonefoundation' ),
             'all_items'          => __( 'All Event', 'alonefoundation' ),
        ),
        'description'        => __( 'Description.', 'alonefoundation' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'event' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => true,
        'taxonomies'            => array('tax_alone_event'),
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail')
    );
    register_post_type( 'alone-event', $alone_event_args );
    $alone_event_tax = array(
        'label' => __( 'Category' ),
        'rewrite' => array( 'slug' => 'event_tax' ),
        'hierarchical' => true,
    ) ;
    register_taxonomy( 'tax_alone_event', array('alone-event'), $alone_event_tax );
}

add_action( 'init', 'Alone_setup_post_type_project');
nmr
  • 309
  • 4
  • 9
Mamunur Rashid
  • 156
  • 2
  • 10

4 Answers4

3

The get_terms() function will only show the default WordPress taxonomies unless you hook it to init.

function get__terms_list() {
  $terms = get_terms('tax_alone_event',array(
    'hide_empty' => false
 ));
  print_r($terms);
} //End get_terms_list()..

//print_r(get__terms_list());

add_action('init','get__terms_list');

Output:

Array ( 
[0] => WP_Term Object ( 
[term_id] => 28 
[name] => Custom Taxonomy 
[slug] => custom-taxonomy 
[term_group] => 0 
[term_taxonomy_id] => 28 
[taxonomy] => tax_alone_event 
[description] => 
[parent] => 0 
[count] => 0 
[filter] => raw )
 )
Gufran Hasan
  • 6,461
  • 7
  • 26
  • 41
2

You should pass taxonomies via the 'taxonomy' argument in the $args array from 4.5.0 - https://developer.wordpress.org/reference/functions/get_terms/.

function get__terms_list() {
    $terms = get_terms( array( 'taxonomy' => 'tax_alone_event' ) );
    return $terms ;
}

print_r(get__terms_list());
Outsource WordPress
  • 3,567
  • 2
  • 7
  • 22
2

You can get rid of errors in two ways

  1. don't use init hook, directly call Alone_setup_post_type_project();
  2. use get_terms function also with init hook

because what you are doing is that using get_terms function before wordpress has registered your post type and taxonomy with the system and thus unable to find it.

Paras
  • 137
  • 3
2

I copied your code to myself and it works well.
In which file do you have this part?

function get__terms_list() {
  $terms = get_terms('tax_alone_event');
  return $terms ;

} //End get_terms_list()..

print_r(get__terms_list());

print_r(get__terms_list()); should be in file like page.php, footer.php, etc.

Put print_r(get_taxonomies()); before print_r(get__terms_list()); to see if your taxonomy is registered.

nmr
  • 309
  • 4
  • 9