Because get_the_terms
will not always return an array; it will be false
if the post has no terms, or a WP_Error
if the taxonomy does not exist – always sanity check the result before looping:
$terms_slugs = array();
$terms = get_the_terms( $post->id, 'type', /* Note: get_the_terms does not take a 3rd argument ==> */ array( 'parent' => 0 ) );
if ( is_array( $terms ) ) {
foreach ( $terms as $term ) {
$terms_slugs[] = $term->slug;
}
}
Better yet, in this case you can use the awesome helper wp_list_pluck()
:
if ( is_array( $terms = get_the_terms( $post->ID, 'type' ) ) )
$terms_slugs = wp_list_pluck( $terms, 'slug' );
else
$terms_slugs = array();
Related Posts:
- Display posts belonging to child term
- List posts by their custom taxonomy term, with a condition
- Get terms parent ID for conditional IF statement
- Copy taxonomy terms from one post to another programmatically
- Add class to first post queried
- Limit Taxonomy Output in Conditional Statement
- Get all terms linked to a post and include those who not connected from get_terms and sort
- wp_insert_post() does not support variable
- How to force acceptance of site terms on first login?
- Get all categories post is not in
- Missing term_id value
- Add HTML to Term Description
- Get the ID of category page with or without any posts
- How can I display a div ONLY if it is not a certain term page
- single_term_title() running before get_the_title() [closed]
- Getting taxonomy images to display on single-post with their terms
- Hide image if taxonomy term is empty
- Get terms within a custom taxonomy
- WordPress Term for Custom List
- array_replace throwing php_warning but working anyway
- Isotope Drop Down Categories Filter Function
- Get current term in single.php
- How to get terms of not only the current cat but also of all its children
- How do I print a term list but alter the link text?
- Ordering taxonomy output in this function
- How to get the $meta_type given the $object_id
- How to filter custom taxonomy term name, slug, and description?
- Polylang : Display term archive even if no posts
- Tax query with multiple terms in pre_get_posts
- Hierarchical taxonomy list with modificated term links
- Advice on creating a WP Archive Template with Custom Taxonomy (get_term_meta)
- $args for get_terms() to return ORDER BY FIELDS
- Is it possible to use array_walk() to append terms to an array of posts?
- Echo term slug op post on archive page
- Exclude product attributes from taxonomy terms loop
- get_terms() returns Trying to get property of non-object error for custom taxonomy
- How to create a array that contains all of the children slugs of a specific parent category
- Getting terms to have space between them
- Get get_terms alphabetically
- Temporarily un-associate term for an amount of time
- Create a a form for custom taxonomy terms
- How to get term by name with parent id?
- How to extract one field from wp_get_post_terms objects?
- Commas not displaying in implode
- Custom term templates
- CTP – check for value inside objects
- If product is in sub-category show code
- INSERTING Data into table with placeholders
- Get Custom Post Child Term
- Set unique term_id from custom meta keys
- Trying to use wp_set_object_terms while supplying an array for both term id, and texonomies
- WordPress custom taxonomy
- Get fuzzy matches from get_term_by
- How to break up php code to avoid echo
- Woocommerce getting top level category parent and make all sub categories have the same template and menu
- Problem retriving the post type category
- PHP displaying wrong custom taxonomy images
- Taxonomy Drop Down with hierarchical view using $terms
- Trying to display terms from custom taxonomy within function
- How to check if a term is parent to another?
- Create Terms on taxonomy by text field
- Customizing the wordpress tag-cloud output
- get_terms_args ordering by meta key not working
- Cannot retrieve categories for post in The Loop
- How to show posts from category A on category B page?
- How to use category slug to echo a page’s content with the same slug
- How do I edit the terms output args or array data?
- Can we apply hide empty to get_the_terms?
- PHP Warning: Attempt to read property “slug” on bool in tax query
- get_terms() delivers wrong count with custom taxonomy childs on custom post type
- update_term_meta() only updating once on certain meta keys
- How to retrieve taxonomy Metabox fields in frontend
- wp_set_object_terms doesn’t work
- Why does term get inserted twice when using dynamic slug?
- How to use get_terms() returning only the terms that have posts with a certain custom field value
- Create category after theme setup and modify the default one
- get_the_term_list( get_the_ID() label formatting?
- I can get posts in all categories. But I want to get the custom post type I created
- $_GET[‘title’] in WordPress
- wp_set_object_terms returns 500 error when called through AJAX function
- how to show all categories by get_terms
- Editing the term_order field
- wp_set_object_terms() to update a post taxonomy value
- Order get_terms by multiple values
- Get All Taxonomy Terms From Get_terms Array With Specific Array Key
- How to display taxonomy order child, parent
- Update term_id to new value wherever it exists, to combine the two.
- wp_get_post_terms – Do Not list Duplicates
- Confused by get_the_terms to use in a new wp_query
- New term taxonomy and description in front page. Input’s
- Add active class to foundation 6 tabs while looping categories
- Flush rewrite rules after a term is edited
- How to show term child only if has a post
- get_terms for all custom taxonomies shows invalid taxonomy error
- wp_set_post_terms struggle :(
- Change the last separator in the_terms
- How to show only tagged CPT categories / taxonomies for a custom post type?
- Is there any WordPress way to get all categories for some selected posts?
- Order terms by creation date
- Unable to retrieve any child terms using get_terms