twork/查询

WP_Query包装器。

2.3.0 2022-02-05 23:26 UTC

This package is auto-updated.

Last update: 2024-09-06 06:32:51 UTC


README

WordPress WP_Query包装器。

安装

  • composer require twork/query

示例用法

use Twork\Query\Query;

$query = new Query('custom-post');

$query->author('Jim')
    ->category('tech')
    ->postsPerPage(20);

foreach ($query as $postId) {
    the_title();
}

在上面的例子中,为自定义文章类型创建了查询,作者为Jim,分类为技术,每页最多显示20篇文章。

如果需要使用不作为方法的参数,可以通过addArg添加,如下面的示例所示。

use Twork\Query\Query;

$query = new Query();

foreach ($query->fetch() as $null) {
    the_title();
}
<?php

class Post {
    protected $id;

    public function __construct() {
        $this->id = get_the_ID();
    }

    public function getId() {
        return $this->id;
    }
}

use Twork\Query\Query;

$query = new Query();

foreach ($query->fetch(Post::class) as $post) {
    echo $post->getId();
}

以下是一个使用addArg添加参数的示例。

$query = new Query('custom-post');

$query->addArg('author_name', 'Jim');