Why “Warning: Invalid argument supplied for foreach()”

According to the documentation, get_the_terms() returns an array of WP_Term objects, or a boolean false value, or a WP_Error object. The last two won’t work with foreach(). You could update your code to check for these conditions: $orgs_in_post = get_the_terms($post[‘ID’], ‘company’); if ( is_array( $orgs_in_post ) ) { foreach ($orgs_in_post as $org_single) { array_push($recent_companies, $org_single->term_id); … Read more

WordPress Related Post by tags in Single.php

The code shown isn’t working as wished, what is wrong? you’re using only the first tag : $first_tag = $tags[0]->term_id; to grab posts from all tags you have to iterate the whole array until a max of 5. The code below ( untested) grabs a max of 25 posts theoretically using up to 5 tags..BUT … Read more

Display the description of taxonomy terms

get_terms() returns an array of WP_Term objects. To get the description, with the usual filters applied, you can pass this object to term_description(): $terms = get_terms( array( ‘taxonomy’ => ‘organizer’, ‘hide_empty’ => false, ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { echo term_description( $term ); } … Read more

Set post categories to include parents when setting child category

This will check to see if there is a hierarchy and set all parent categories as checked. This function assumes that if there’s multiple categories already set, then don’t do the function, since the categories would be correct if multiples are set. function set_product_parent_categories( $post_id ) { $category = wp_get_post_terms( $post_id, ‘product_cat’ ); // If … Read more

Set post terms on post publish

The argument from pending_to_publish action, $post_id in your case, is not ID but array (callback to publish). I think it would be better to use the publish_post action, that way $post_id will actualy be the post id. Also this is a very general action so if you have multiple post types it would be wise … Read more