Skip to content

Commit fb8500f

Browse files
committed
Add ability to get post categories along with post
1 parent a547f51 commit fb8500f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/PostTypes/Post.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace OOPWP\PostTypes;
44

55
use Carbon\Carbon;
6+
use OOPWP\Terms\Category;
7+
use WP_Term;
68

79
class Post
810
{
@@ -17,6 +19,12 @@ class Post
1719
public string $status = '';
1820
public string $format = '';
1921
public string $excerpt = '';
22+
23+
/**
24+
* @var Category[]
25+
*/
26+
public array $categories;
27+
2028
public Carbon $publishedDate;
2129
public Carbon $modifiedDate;
2230
public \WP_Post $parent;
@@ -44,6 +52,7 @@ public function withAll()
4452
->withStatus()
4553
->withFormat()
4654
->withExcerpt()
55+
->withCategories()
4756
->withPublishedDate()
4857
->withModifiedDate()
4958
->withParent();
@@ -86,6 +95,15 @@ public function withExcerpt()
8695
return $this;
8796
}
8897

98+
public function withCategories()
99+
{
100+
$this->categories = \array_map(function (WP_Term $term) {
101+
return new Category($term->term_id);
102+
}, \get_the_terms($this->WP_Post, 'category'));
103+
104+
return $this;
105+
}
106+
89107
public function withStatus()
90108
{
91109
$this->status = $this->WP_Post->post_status;

0 commit comments

Comments
 (0)