This is a wp-kit component that handles Eloquent
appending and querying of PostMeta
., (*1)
When using Eloquent
, wouldn't it be great if we could append PostMeta
to the Model just as Wordpress does natively? And wouldn't be great to query data based on meta_query
and tax_query
parameters? This is exactly what wp-kit/magic-meta
handles., (*2)
Installation
If you're using Themosis
, install via Composer
in the root of your Themosis
installation, otherwise install in your Composer
driven theme folder:, (*3)
composer require "wp-kit/magic-meta"
Usage
Model
wp-kit/magic-meta
comes with two traits, so all you need to do is include these in your model., (*4)
Based on drewjbartlett/wordpress-eloquent
you can the Post
model provided and use the IsMagic
trait., (*5)
wp-kit/magic-meta
relies on a ::getMeta
method on the Model to return the meta_value
, this is exactly what drewjbartlett/wordpress-eloquent
provides., (*6)
namespace Theme\Models;
use WPEloquent\Model\Post;
use WPKit\MagicMeta\Traits\IsMagic;
use WPKit\MagicMeta\Traits\TransformsQuery;
class SomePostType extends Post {
use IsMagic;
use TransformsQuery;
protected $magic_meta = [
'_some_meta_key' => 'appended_key',
'_location' => 'location'
];
}
Query
Parameters, (*7)
You can use ::transformQuery
Query Scope on Illuminate\Database\Query\Builder
to check for any of the following parameters. We also allow the query to check for any magic meta at root level of the parameters:, (*8)
[
's' => '',
'meta_query' => [],
'tax_query' => [],
'appended_key' => 'something', // queries PostMeta key '_some_meta_key'
'location' => 'london' // queries PostMeta key '_location'
]
Using transformQuery, (*9)
namespace App\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Models\SomePostType;
class SomePostTypeController extends Controller {
public function index(Request $request) {
return response()->json( SomePostType::select( 'posts.*' )->type( 'some_type' )->transformQuery( $request ) );
}
}
To Do
- Make transformQuery agnostic to Request parameters, using Collection instead in case users are using jsonapi standard etc.
Get Involved
To learn more about how to use wp-kit
check out the docs:, (*10)
View the Docs, (*11)
Any help is appreciated. The project is open-source and we encourage you to participate. You can contribute to the project in multiple ways by:, (*12)
- Reporting a bug issue
- Suggesting features
- Sending a pull request with code fix or feature
- Following the project on GitHub
- Sharing the project around your community
For details about contributing to the framework, please check the contribution guide., (*13)
Requirements
Wordpress 4+, (*14)
PHP 5.6+, (*15)
License
wp-kit/magic-meta is open-sourced software licensed under the MIT License., (*16)