Fetch posts with Bazooka

To get single or multiple posts you can use PostRepository class. This brings to you 2 methods find(int $id) and findAll().

Examples with find(int $id)

<?php 

use Fantassin\Core\WordPress\PostType\Repository\PostRepository;

$repository = new PostRepository();
$currentPost = $repository->find( get_the_ID() );

In this example, I get current single post with get_the_ID() as ID. By default, find() method return Post entity.

Examples with findAll()

<?php 

use Fantassin\Core\WordPress\PostType\Repository\PostRepository;

$repository = new PostRepository();
$allPosts = $repository->findAll();

In this example, I get all posts from blog, the return of findAll() method is array of Post entity.

Post Entity

You can get basic informations from post element with right typing :

  • getId() : integer
  • getSlug() : string
  • getTitle() : string
  • getAuthor() : string
  • getContent() : string
  • getParentId() : integer
  • getPostType() : string
  • getPublishedAt() : DateTime Interface
  • getModifiedAt() : DateTime Interface

In the same way you can fetch terms with strict typing.