/** * Gets all the posts terms * * @param array $posts * * @return array|null|object */ protected function get_terms(array $posts = []) { if (empty($posts)) { return []; } global $wpdb; $ids = []; $placeholders = []; $cast_terms = []; $sql = "SELECT "; $sql .= "{$wpdb->term_relationships}.object_id as post_id, "; $sql .= "{$wpdb->term_taxonomy}.taxonomy, "; $sql .= "{$wpdb->terms}.term_id, "; $sql .= "{$wpdb->terms}.name, "; $sql .= "{$wpdb->terms}.slug, "; $sql .= "{$wpdb->term_taxonomy}.description, "; $sql .= "{$wpdb->term_taxonomy}.parent, "; $sql .= "{$wpdb->term_taxonomy}.count, "; $sql .= "{$wpdb->terms}.term_group, "; $sql .= "{$wpdb->term_relationships}.term_order "; $sql .= "FROM {$wpdb->term_relationships} "; $sql .= "JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id "; $sql .= "JOIN {$wpdb->terms} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id "; $sql .= "WHERE {$wpdb->term_relationships}.object_id IN ("; foreach ($posts as $post) { if ($post instanceof WP_Post) { $placeholders[] = "%d"; $ids[] = $post->ID; } } $sql .= join($placeholders, ', ') . ") "; $terms = $wpdb->get_results($wpdb->prepare($sql, $ids)); foreach ($terms as $term) { $cast_terms[] = new WP_Term($term); } return $this->term_transformer->prepare_collection($cast_terms); }