### Install WP-ORM using Composer Source: https://github.com/dimitribouteille/wp-orm/wiki/Home This snippet shows how to install the wp-orm library using Composer, a dependency manager for PHP. Ensure you have Composer installed and follow the standard installation instructions. ```bash composer require dbout/wp-orm ``` -------------------------------- ### Install illuminate/events Package Source: https://github.com/dimitribouteille/wp-orm/wiki/Events Installs the 'illuminate/events' package using Composer, which is required to enable Eloquent event dispatching in the WP ORM library. This package provides the necessary event dispatcher functionality. ```bash composer require illuminate/events ^11.0 ``` -------------------------------- ### DB Facade Setup and Usage in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Explains how to enable and use Laravel's DB facade for raw SQL queries and advanced database operations within WordPress. It covers initialization, executing selects, counting records, updating, and using raw expressions. ```php use Dbout\WpOrm\Orm\Database; use Illuminate\Container\Container; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\DB; // Initialize the container (typically in a mu-plugin) $container = new Container(); $container->instance('db', Database::getInstance()); Facade::setFacadeApplication($container); // Use the DB facade $results = DB::select('SELECT * FROM wp_posts WHERE post_status = ?', ['publish']); $count = DB::table('posts') ->where('post_status', 'publish') ->count(); DB::table('postmeta') ->where('post_id', 123) ->update(['meta_value' => 'new value']); // Raw expressions $post = Post::find(123); $post->comment_count = DB::raw('comment_count + 1'); $post->save(); ``` -------------------------------- ### Include Autoloader in wp-config.php Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Example of how to include the Composer autoloader in your wp-config.php file to enable the WordPress ORM library. ```php // In wp-config.php require __DIR__ . '/vendor/autoload.php'; ``` -------------------------------- ### Setup Event Dispatcher in AbstractModel Child Class Source: https://github.com/dimitribouteille/wp-orm/wiki/Events Demonstrates how to set up the event dispatcher in a child class extending the AbstractModel. This involves calling `setEventDispatcher` with a new instance of `Illuminate\Events\Dispatcher` within the `boot` method. This is crucial for enabling event functionality. ```php use Illuminate\Events\Dispatcher; use Dbout\WpOrm\Orm\AbstractModel; class User extends AbstractModel { protected static function boot() { static::setEventDispatcher(new Dispatcher()); parent::boot(); } } ``` -------------------------------- ### Eloquent Event Handling in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Illustrates how to enable and utilize Eloquent model lifecycle events (creating, saving, deleting, etc.) in PHP. It provides an example of a Product model with listeners for various events, demonstrating actions like setting user IDs, updating timestamps, cache invalidation, and data cleanup. ```php use Illuminate\Events\Dispatcher; use Dbout\WpOrm\Orm\AbstractModel; // Set up events in your model class Product extends AbstractModel { protected $table = 'products'; protected static function boot() { static::setEventDispatcher(new Dispatcher()); parent::boot(); // Register event listeners static::creating(function (Product $product) { $product->setAttribute('created_by', get_current_user_id()); }); static::saving(function (Product $product) { $product->setAttribute('updated_at', now()); }); static::saved(function (Product $product) { // Clear cache, send notifications, etc. wp_cache_delete('product_' . $product->getId()); }); static::deleting(function (Product $product) { // Clean up related data $product->metas()->delete(); }); } } // Available events: // retrieved, creating, created, updating, updated, // saving, saved, deleting, deleted, trashed, // forceDeleting, forceDeleted, restoring, restored, replicating ``` -------------------------------- ### Apply Reusable Filters to Queries Using Taps in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Illustrates how to use 'tap' classes in PHP to apply common filters to database queries in a reusable manner. This promotes cleaner code by encapsulating filtering logic, shown with examples for filtering posts, comments, options, and attachments. ```php use Dbout\WpOrm\Models\Post; use Dbout\WpOrm\Models\Comment; use Dbout\WpOrm\Models\Option; use Dbout\WpOrm\Models\Attachment; use Dbout\WpOrm\Taps\Post\IsAuthorTap; use Dbout\WpOrm\Taps\Post\IsStatusTap; use Dbout\WpOrm\Taps\Post\IsPostTypeTap; use Dbout\WpOrm\Taps\Post\IsPingStatusTap; use Dbout\WpOrm\Taps\Comment\IsApprovedTap; use Dbout\WpOrm\Taps\Comment\IsUserTap; use Dbout\WpOrm\Taps\Comment\IsCommentTypeTap; use Dbout\WpOrm\Taps\Option\IsAutoloadTap; use Dbout\WpOrm\Taps\Attachment\IsMimeTypeTap; use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Enums\PingStatus; use Dbout\WpOrm\Enums\YesNo; // Filter posts by author $posts = Post::query() ->tap(new IsAuthorTap(1)) ->get(); // Combine multiple taps $publishedByAuthor = Post::query() ->tap(new IsAuthorTap(1)) ->tap(new IsStatusTap(PostStatus::Publish)) ->tap(new IsPingStatusTap(PingStatus::Open)) ->get(); // Filter comments $approvedComments = Comment::query() ->tap(new IsApprovedTap()) ->tap(new IsUserTap(1)) ->get(); // Filter options $autoloadOptions = Option::query() ->tap(new IsAutoloadTap(YesNo::Yes)) ->get(); // Filter attachments by MIME type $images = Attachment::query() ->tap(new IsMimeTypeTap('image/jpeg')) ->get(); ``` -------------------------------- ### Create Custom Models for Custom Database Tables in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Provides a guide on creating custom models in PHP for non-standard database tables by extending the `AbstractModel` class. It details how to specify the table name, primary key, enable timestamps, and define attribute casting for model properties. ```php use Dbout\WpOrm\Orm\AbstractModel; // Define a model for a custom table class Order extends AbstractModel { protected $table = 'orders'; // Table name (prefix added automatically) protected $primaryKey = 'order_id'; // Primary key column public $timestamps = true; // Enable created_at/updated_at // Define attribute casting protected $casts = [ 'total' => 'float', 'is_paid' => 'boolean', 'order_date' => 'datetime', 'items' => 'array', ]; } // Use the custom model $order = new Order(); $order->setAttribute('customer_id', 123); $order->setAttribute('total', 99.99); $order->setAttribute('is_paid', false); $order->save(); // Query custom table $pendingOrders = Order::query() ->where('is_paid', false) ->orderBy('order_date', 'desc') ->get(); // Find by ID $order = Order::find(456); ``` -------------------------------- ### Manage Metadata with Post and User Models Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Models that support metadata, such as Post and User, offer methods for retrieving, setting, and deleting meta values. This includes getting raw values, model instances, checking for meta existence, and setting meta on new or existing models. ```php use Dbout\WpOrm\Models\Post; use Dbout\WpOrm\Models\User; // Get meta values $post = Post::find(123); $price = $post->getMetaValue('_price'); // Returns raw value $meta = $post->getMeta('_price'); // Returns PostMeta model $hasMeta = $post->hasMeta('_price'); // Returns boolean // Set meta values $post->setMeta('_price', '29.99'); $post->setMeta('_featured', true); // Delete meta $post->deleteMeta('_old_meta_key'); // Set meta on new (unsaved) models - saved automatically when model is saved $newPost = new Post(); $newPost->setPostTitle('New Post') ->setMeta('_custom_field', 'value'); // Stored temporarily $newPost->save(); // Meta is saved after post // User meta operations work the same way $user = User::find(1); $user->setMeta('billing_address', '123 Main St'); $address = $user->getMetaValue('billing_address'); ``` -------------------------------- ### Filter Posts by Author and Status using Tap Source: https://github.com/dimitribouteille/wp-orm/wiki/Filter-data Shows how to apply multiple filters to a query using the `tap` method. This example filters posts by a specific author ID and a defined post status. ```php use Dbout\WpOrm\Taps\Post\IsAuthorTap; use Dbout\WpOrm\Taps\Post\IsStatusTap; use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Models\Post; $posts = Post::query() ->tap(new IsAuthorTap(1)) ->tap(new IsStatusTap(PostStatus::Publish)) ->get(); ``` -------------------------------- ### Define Generic Model with Custom Table (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Create-custom-model This example demonstrates how to specify a custom table name for a generic model by setting the protected $table property. This overrides the default table naming convention. ```php use Dbout\WpOrm\Orm\AbstractModel; class MyModel extends AbstractModel { protected $table = 'my_table'; } ``` -------------------------------- ### Registering a Saved Event Listener with Closure Source: https://github.com/dimitribouteille/wp-orm/wiki/Events Shows how to register a listener for the 'saved' event using a closure. This code snippet extends the previous setup by adding custom logic that will be executed after a model is successfully saved. The closure receives the model instance as an argument. ```php use Illuminate\Events\Dispatcher; use Dbout\WpOrm\Orm\AbstractModel; class User extends AbstractModel { protected static function boot() { static::setEventDispatcher(new Dispatcher()); parent::boot(); static::saved(function (User $user) { // Add your logic here }); } } ``` -------------------------------- ### Include WP-ORM Autoloader in PHP Source: https://github.com/dimitribouteille/wp-orm/wiki/Home This snippet demonstrates how to include the Composer autoloader in your PHP script to make the wp-orm library's classes available. This is a standard practice after installing libraries via Composer. ```php require __DIR__ . '/vendor/autoload.php'; ``` -------------------------------- ### Filter Posts using Eloquent Query Builder Source: https://github.com/dimitribouteille/wp-orm/wiki/Filter-data Illustrates how to filter posts using the standard Eloquent query builder. This example filters posts based on their `ping_status` attribute. ```php use Dbout\WpOrm\Models\Post; $posts = Post::query() ->where('ping_status', 'closed') ->get(); ``` -------------------------------- ### Create and Query Custom Comment Type Models Source: https://context7.com/dimitribouteille/wp-orm/llms.txt This section explains how to create models for custom comment types by extending the CustomComment class. It includes examples of defining a custom type and performing queries and creations with automatic type filtering. ```php use Dbout\WpOrm\Models\CustomComment; // Define a custom comment type model class Review extends CustomComment { protected string $_type = 'review'; } // Query reviews $reviews = Review::query() ->where('comment_approved', '1') ->get(); // Automatically filters by comment_type = 'review' // Create a new review $review = new Review(); $review->setCommentPostID(123) ->setCommentContent('5 stars!') ->setCommentApproved('1') ->save(); // comment_type is automatically set to 'review' ``` -------------------------------- ### Multisite Support in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Demonstrates querying and accessing data from WordPress multisite tables using the Blog, Site, and Signup models. It shows how to retrieve blogs based on criteria and access related site and version information. ```php use Dbout\WpOrm\Models\Multisite\Blog; use Dbout\WpOrm\Models\Multisite\Site; use Dbout\WpOrm\Models\Multisite\Signup; use Dbout\WpOrm\Models\Multisite\BlogVersion; // Query blogs in a multisite network $blogs = Blog::query() ->where('public', true) ->where('deleted', false) ->where('spam', false) ->get(); // Access blog relationships $blog = Blog::find(2); $site = $blog->site; // Site model $version = $blog->version; // BlogVersion model // Get blog properties $domain = $blog->getDomain(); $path = $blog->getPath(); $registered = $blog->getRegistered(); // Carbon instance // Query sites $sites = Site::query()->get(); // Query pending signups $pendingSignups = Signup::query() ->where('active', false) ->get(); ``` -------------------------------- ### Initialize Container and Facades (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/DB-facade This snippet demonstrates how to initialize the Laravel service container and set up facades for use within a WordPress mu-plugin. It requires the `Dbout\WpOrm\Orm\Database` and `Illuminate\Container\Container` classes. ```php use Dbout\WpOrm\Orm\Database; use Illuminate\Container\Container; use Illuminate\Support\Facades\Facade; $container = new Container(); $container->instance('db', Database::getInstance()); Facade::setFacadeApplication($container); ``` -------------------------------- ### WordPress User Model Operations Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Illustrates how to use the User model for finding, creating, querying, accessing relationships, and retrieving user attributes. ```php use Dbout\WpOrm\Models\User; // Find users $user = User::find(1); $user = User::findOneByEmail('user@example.com'); $user = User::findOneByLogin('admin'); // Create a new user $user = new User(); $user->setUserLogin('newuser') ->setUserPass(wp_hash_password('password123')) ->setUserEmail('newuser@example.com') ->setUserNicename('newuser') ->setDisplayName('New User') ->save(); // Query users $admins = User::query() ->where('user_status', 0) ->orderBy('user_registered', 'desc') ->get(); // Access relationships $user = User::find(1); $posts = $user->posts; // Collection of Post models $comments = $user->comments; // Collection of Comment models $metas = $user->metas; // Collection of UserMeta models // Get user attributes $email = $user->getUserEmail(); $displayName = $user->getDisplayName(); $registeredDate = $user->getUserRegistered(); // Carbon instance ``` -------------------------------- ### WordPress Post Model Operations Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Demonstrates how to use the Post model for finding, creating, querying, accessing relationships, updating, and deleting WordPress posts. ```php use Dbout\WpOrm\Models\Post; use Dbout\WpOrm\Enums\PostStatus; // Find a post by ID $post = Post::find(123); // Find by slug or GUID $post = Post::findOneByName('my-post-slug'); $post = Post::findOneByGuid('https://example.com/?p=123'); // Create a new post $post = new Post(); $post->setPostTitle('My New Post') ->setPostContent('

Content here

') ->setPostStatus('publish') ->setPostType('post') ->setPostAuthor(1) ->save(); // Query posts with Eloquent $publishedPosts = Post::query() ->where('post_status', 'publish') ->where('post_type', 'post') ->orderBy('post_date', 'desc') ->limit(10) ->get(); // Access relationships $post = Post::find(1); $author = $post->author; // User model $comments = $post->comments; // Collection of Comment models $parent = $post->parent; // Parent Post model $metas = $post->metas; // Collection of PostMeta models // Update a post $post = Post::find(123); $post->setPostTitle('Updated Title')->save(); // Delete a post Post::find(123)->delete(); ``` -------------------------------- ### Use DB Facade for Database Operations (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/DB-facade This snippet shows how to utilize the `DB` facade after the container has been initialized. It allows for easy database interactions, such as incrementing a count. ```php use \Illuminate\Support\Facades\DB; $count = DB::raw('count + 1'); ``` -------------------------------- ### WordPress Comment Model Operations Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Shows how to use the Comment model for finding, creating, querying, accessing relationships, and retrieving comment attributes. ```php use Dbout\WpOrm\Models\Comment; // Find a comment $comment = Comment::find(456); // Create a new comment $comment = new Comment(); $comment->setCommentPostID(123) ->setCommentAuthor('John Doe') ->setCommentAuthorEmail('john@example.com') ->setCommentContent('Great post!') ->setCommentApproved('1') ->setUserId(1) ->save(); // Query comments $approvedComments = Comment::query() ->where('comment_approved', '1') ->where('comment_post_ID', 123) ->orderBy('comment_date', 'desc') ->get(); // Access relationships $comment = Comment::find(456); $post = $comment->post; // Related Post model $user = $comment->user; // Related User model $parent = $comment->parent; // Parent Comment model // Get comment attributes $content = $comment->getCommentContent(); $date = $comment->getCommentDate(); // Carbon instance $authorIP = $comment->getCommentAuthorIP(); ``` -------------------------------- ### Manage WordPress Options with Option Model Source: https://context7.com/dimitribouteille/wp-orm/llms.txt The Option model allows for CRUD operations on WordPress options stored in the 'options' table. It supports finding, creating, updating, querying, and deleting options. Dependencies include the Option model and the YesNo enum. ```php use Dbout\WpOrm\Models\Option; use Dbout\WpOrm\Enums\YesNo; // Find an option by name $option = Option::findOneByName('blogname'); $blogName = $option->getOptionValue(); // Create or update an option $option = Option::findOneByName('my_custom_option'); if (!$option) { $option = new Option(); $option->setOptionName('my_custom_option'); } $option->setOptionValue('my_value') ->setAutoload(YesNo::Yes) ->save(); // Query options $autoloadOptions = Option::query() ->where('autoload', 'yes') ->get(); // Delete an option Option::findOneByName('my_custom_option')?->delete(); ``` -------------------------------- ### Define and Use Custom Post Type Models Source: https://context7.com/dimitribouteille/wp-orm/llms.txt This snippet demonstrates how to create custom models for WordPress custom post types by extending the CustomPost class. It shows how to define the post type and use the model for querying and creating posts. The post_type filter is automatically applied. ```php use Dbout\WpOrm\Models\CustomPost; // Define a custom post type model class Product extends CustomPost { protected string $_type = 'product'; } // Use the custom post type model $products = Product::query()->get(); // Automatically filters by post_type = 'product' // Create a new product $product = new Product(); $product->setPostTitle('New Product') ->setPostContent('Product description') ->setPostStatus('publish') ->save(); // post_type is automatically set to 'product' // Built-in custom post types use Dbout\WpOrm\Models\Article; // post_type = 'post' use Dbout\WpOrm\Models\Page; // post_type = 'page' use Dbout\WpOrm\Models\Attachment; // post_type = 'attachment' $pages = Page::query()->where('post_status', 'publish')->get(); $articles = Article::query()->limit(5)->get(); ``` -------------------------------- ### Interact with WordPress Terms using Term Model Source: https://context7.com/dimitribouteille/wp-orm/llms.txt The Term model provides access to WordPress taxonomy terms and their relationships with term taxonomies. It supports finding, creating, and querying terms. Dependencies include the Term and TermTaxonomy models. ```php use Dbout\WpOrm\Models\Term; use Dbout\WpOrm\Models\TermTaxonomy; // Find a term $term = Term::find(5); // Create a new term $term = new Term(); $term->setName('Technology') ->setSlug('technology') ->save(); // Access term taxonomy relationship $term = Term::find(5); $taxonomy = $term->termTaxonomy; // TermTaxonomy model // Query terms $terms = Term::query() ->where('slug', 'like', 'tech%') ->get(); ``` -------------------------------- ### WordPress Status Enums in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Showcases the use of built-in PHP enums provided by the library for common WordPress status values like post status, ping status, and yes/no flags. It demonstrates accessing enum values and using them in database queries. ```php use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Enums\PingStatus; use Dbout\WpOrm\Enums\YesNo; use Dbout\WpOrm\Models\Post; // PostStatus enum values PostStatus::Publish; // 'publish' PostStatus::Draft; // 'draft' PostStatus::Pending; // 'pending' PostStatus::Private; // 'private' PostStatus::Trash; // 'trash' PostStatus::Future; // 'future' PostStatus::AutoDraft; // 'auto-draft' PostStatus::Inherit; // 'inherit' // Use enums in queries $drafts = Post::query() ->where('post_status', PostStatus::Draft->value) ->get(); // PingStatus enum values PingStatus::Open; // 'open' PingStatus::Closed; // 'closed' // YesNo enum values YesNo::Yes; // 'yes' YesNo::No; // 'no' ``` -------------------------------- ### Define Generic Model Extending AbstractModel (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Create-custom-model This snippet shows how to create a basic model by extending the AbstractModel class. By default, Eloquent infers the table name from the model's class name (snake_case, plural). ```php use Dbout\WpOrm\Orm\AbstractModel; class MyModel extends AbstractModel { } ``` -------------------------------- ### Filter Posts by Author using Tap Source: https://github.com/dimitribouteille/wp-orm/wiki/Filter-data Demonstrates how to filter posts to retrieve only those authored by a specific user ID using the `tap` method with `IsAuthorTap`. This method allows for modular query building. ```php use Dbout\WpOrm\Taps\Post\IsAuthorTap; use Dbout\WpOrm\Models\Post; $posts = Post::query() ->tap(new IsAuthorTap(1)) ->get(); ``` -------------------------------- ### Filter Models by Meta Values Using Query Builder in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Explains how to filter models based on their meta values directly within the query builder in PHP. It covers simple meta value filtering, filtering with operators, combining meta filters with regular query conditions, and selecting meta values in query results. ```php use Dbout\WpOrm\Models\Post; use Dbout\WpOrm\Models\User; // Filter posts by meta value $featuredProducts = Post::query() ->addMetaToFilter('_featured', '1') ->get(); // Filter with operators $expensiveProducts = Post::query() ->addMetaToFilter('_price', 100, '>=') ->get(); // Combine meta filters with regular filters $publishedFeatured = Post::query() ->where('post_status', 'publish') ->where('post_type', 'product') ->addMetaToFilter('_featured', '1') ->addMetaToFilter('_stock_quantity', 0, '>') ->get(); // Select meta values in query results $products = Post::query() ->addMetaToSelect('_price', 'price') ->addMetaToSelect('_sku', 'sku') ->where('post_type', 'product') ->get(); // Access selected meta values foreach ($products as $product) { echo $product->price; // Meta value from _price echo $product->sku; // Meta value from _sku } // Select multiple metas at once $products = Post::query() ->addMetasToSelect([ 'price' => '_price', 'sku' => '_sku', '_stock_quantity', // Alias defaults to 'stock_quantity_value' ]) ->get(); ``` -------------------------------- ### Define Custom Post Type Model (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Create-custom-model This code illustrates how to create a model for a Custom Post Type by extending the CustomPost class and defining the $_type property. This automatically applies a 'post_type' filter when querying and sets the 'post_type' property when creating new instances. ```php use Dbout\WpOrm\Models\CustomPost; class MyCustomType extends CustomPost { /** * @inheritDoc */ protected string $_type = 'my_customm_type'; } ``` -------------------------------- ### Define Meta Casting for Model Attributes in PHP Source: https://context7.com/dimitribouteille/wp-orm/llms.txt Demonstrates how to define meta casts within a PHP model to automatically convert meta values to specific data types (e.g., float, integer, boolean, array, JSON, datetime, or Enum classes) when accessing them. This enhances data integrity and simplifies type handling. ```php use Dbout\WpOrm\Models\Post; // Define a model with meta casting class Product extends Post { protected function metaCasts(): array { return [ '_price' => 'float', '_stock_quantity' => 'integer', '_is_featured' => 'boolean', '_attributes' => 'array', '_settings' => 'json', '_sale_start' => 'datetime', '_status' => ProductStatus::class, // Enum casting ]; } } // Using cast metas $product = Product::find(123); $price = $product->getMetaValue('_price'); // Returns float $stock = $product->getMetaValue('_stock_quantity'); // Returns int $featured = $product->getMetaValue('_is_featured'); // Returns bool $attrs = $product->getMetaValue('_attributes'); // Returns array $saleStart = $product->getMetaValue('_sale_start'); // Returns Carbon instance // Supported cast types: // 'array', 'bool', 'boolean', 'collection', 'date', 'datetime', // 'double', 'float', 'immutable_date', 'int', 'integer', // 'json', 'object', 'string', 'timestamp', Enum classes ``` -------------------------------- ### Meta Casting with `metaCasts` Method (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Attribute-&-meta-casting Demonstrates meta casting in WP ORM by transforming meta values when retrieved or set. The `metaCasts` method returns an array mapping meta keys to desired cast types (e.g., 'is_admin' to 'boolean'). Null meta values are not cast. ```php namespace App\Models; use Dbout\WpOrm\Models\Post; class User extends Post { protected function metaCasts(): array { return [ 'is_admin' => 'boolean', ]; } } ``` -------------------------------- ### Filter Posts by Meta Field using addMetaToFilter Source: https://github.com/dimitribouteille/wp-orm/wiki/Filter-data Demonstrates filtering posts based on a specific meta field and its value using the `addMetaToFilter` method. This is useful for models with associated meta data. ```php use Dbout\WpOrm\Models\Post; $products = Post::query() ->addMetaToFilter('product_type', 'simple') ->get(); ``` -------------------------------- ### Attribute Casting in Eloquent Models (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Attribute-&-meta-casting Defines how to cast Eloquent model attributes to specific data types using the `$casts` property. This ensures attributes like booleans, arrays, or datetimes are handled correctly. It's often mandatory unless the database driver automatically handles casting. ```php class User extends Model { protected $casts = [ 'is_admin' => 'boolean', 'settings' => 'array', 'created_at' => 'datetime', ]; } ``` -------------------------------- ### Meta Casting with `$metaCasts` Property (PHP) Source: https://github.com/dimitribouteille/wp-orm/wiki/Attribute-&-meta-casting An alternative method for meta casting in WP ORM using the `$metaCasts` property, similar to attribute casting. This allows defining meta value transformations directly as a class property. Null meta values are not cast. ```php namespace App\Models; use Dbout\WpOrm\Models\Post; class User extends Post { protected $metaCasts = [ 'is_admin' => 'boolean', ]; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.