PHP code example of thewildfields / tavriia

1. Go to this page and download the library: Download thewildfields/tavriia library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thewildfields / tavriia example snippets


$post = get_post($id);
if (!$post instanceof \WP_Post) {
    // handle missing
}
$meta = get_post_meta($id, 'some_field', true);
$result = wp_insert_post($args);
if (is_wp_error($result)) {
    // handle error
}

$post = $this->postRepository->findById($id); // throws PostNotFoundException or returns PostDTO
$meta = $this->postRepository->metaFor($id)->getString('some_field');
$newId = $this->postFactory->create($dto); // throws or returns int

use TheWildFields\Tavriia\AbstractModule;
use TheWildFields\Tavriia\Post\PostFactory;
use TheWildFields\Tavriia\Post\PostRepository;

final class EventsModule extends AbstractModule
{
    public function __construct(
        private readonly PostFactory $postFactory,
        private readonly PostRepository $postRepository,
    ) {}

    public function register_hooks(): void
    {
        add_action('init', [$this, 'registerPostType']);
        add_action('save_post_event', [$this, 'onSaveEvent'], 10, 2);
    }
}

use TheWildFields\Tavriia\DTO\PostDTO;

$dto = new PostDTO(
    title: 'Summer Concert',
    content: 'Annual summer concert in the park.',
    status: 'publish',
    postType: 'event',
    meta: ['venue_id' => 42],
);

$eventId = $this->postFactory->create($dto);

use TheWildFields\Tavriia\Query\QueryBuilder;

$results = (new QueryBuilder())
    ->postType('event')
    ->metaQuery('venue_id', 42)
    ->orderBy('date', 'DESC')
    ->limit(10)
    ->get();

foreach ($results as $post) {
    echo $post->title;
}

use TheWildFields\Tavriia\Http\RequestBuilder;
use TheWildFields\Tavriia\Http\HttpClient;
use TheWildFields\Tavriia\Http\ResponseProcessor;

$client = new HttpClient(new ResponseProcessor());

$response = $client->get('https://api.example.com/events');
$data = $response->json();