mehedimi/wp-query-builder-ext

WP Query Builder 扩展

0.3 2024-02-11 00:53 UTC

This package is auto-updated.

Last update: 2024-09-16 14:07:54 UTC


README

WP Query Builder Extension Banner

WP Query Builder 扩展

这是 WP Query Builder 的一个扩展。

安装

它是一个 composer 包。您可以使用 composer 执行以下命令来安装。

composer require mehedimi/wp-query-builder-ext

它与 WP Query Builder 的一些关系和插件相关。

关系

WithTaxonomy

使用这个关系,您将能够加载特定文章的关联分类。

// Retrieve all posts with associative taxonomies.
DB::table('posts')
->withRelation(new WithTaxonomy('taxonomies'))
->get()

如果您需要按分类类型分组,则只需在 WithTaxonomy 关系上调用 groupByTaxonomy 方法。

// Retrieve all posts with associative taxonomies group by with its type.
DB::table('posts')
->withRelation(new WithTaxonomy('taxonomies'), function(WithTaxonomy $taxonomy){
    $taxonomy->groupByTaxonomy()
})
->get();

可选地,您可以通过调用 WithTaxonomy 关系的 taxonomy 方法添加分类类型的约束。

// This will fetch only category type of taxonomy.
DB::table('posts')
    ->withRelation(new WithTaxonomy('categories'), function(WithTaxonomy $taxonomy){
        $taxonomy->taxonomy('category');
    })->get();

插件

JoinPostWithMeta

使用这个插件,您可以非常容易地将 postmeta 表与 posts 表连接起来。您只需应用此插件即可。下面给出了一些示例

DB::plugin(new JoinPostWithMeta())->select('posts.*')->where('meta_key', 'some meta key name')->get()

您还可以在 JoinPostWithMeta 类构造函数中提供连接类型。

// For joining right join `postmeta` with `posts` table
DB::plugin(new JoinPostWithMeta('right'))->select('posts.*')->where('meta_key', 'some meta key name')->get()