I'm trying to deduce a category for my custom WordPress post, but I get this error. How to fix ?
$events= wp_get_post_terms(get_the_ID(), 'events', array("fields" => "all"));
<?php echo $events[0]->name ?>
add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
$labels = array(
"name" => __( 'Events', '' ),
"singular_name" => __( 'event', '' ),
);
$args = array(
// HERE
"label" => __( 'Events', '' ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "events", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "excerpt", "revisions" ), );
register_post_type( "events", $args );
// End of cptui_register_my_cpts()
}
I'm trying to deduce a category for my custom WordPress post, but I get this error. How to fix ?
$events= wp_get_post_terms(get_the_ID(), 'events', array("fields" => "all"));
<?php echo $events[0]->name ?>
add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
$labels = array(
"name" => __( 'Events', '' ),
"singular_name" => __( 'event', '' ),
);
$args = array(
// HERE
"label" => __( 'Events', '' ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "events", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "excerpt", "revisions" ), );
register_post_type( "events", $args );
// End of cptui_register_my_cpts()
}
Share
Improve this question
edited Jan 29, 2020 at 6:49
kaizer
asked Jan 29, 2020 at 6:06
kaizerkaizer
11 bronze badge
1
- You have not added 'taxonomies' => array('topics', 'category' ), in args array() – Dharmishtha Patel Commented Jan 29, 2020 at 7:24
1 Answer
Reset to default 0wp_get_post_terms()
will return a WP_Error
object if the taxonomy can't be found. So $events[0]
won't exist if that happens.
If you're getting this error it means that there's no taxonomy named events
registered on your site. So you'll need to make sure that you've used the correct name for the taxonomy whose terms you want to list.