This is a wp-kit component that handles Eloquent
appending and querying of PostMeta
.
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.
If you're using Themosis
, install via Composer
in the root of your Themosis
installation, otherwise install in your Composer
driven theme folder:
composer require "wp-kit/magic-meta"
wp-kit/magic-meta
comes with two traits, so all you need to do is include these in your model.
Based on drewjbartlett/wordpress-eloquent
you can the Post
model provided and use the IsMagic
trait.
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.
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'
];
}
Parameters
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:
[
's' => '',
'meta_query' => [],
'tax_query' => [],
'appended_key' => 'something', // queries PostMeta key '_some_meta_key'
'location' => 'london' // queries PostMeta key '_location'
]
Using transformQuery
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 ) );
}
}
- Make transformQuery agnostic to Request parameters, using Collection instead in case users are using jsonapi standard etc.
To learn more about how to use wp-kit
check out the docs:
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:
- 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.
Wordpress 4+
PHP 5.6+
wp-kit/magic-meta is open-sourced software licensed under the MIT License.