Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
.idea/
61 changes: 37 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ INSTALLATION
USAGE
=====

### META QUERIES
### 元数据查询


Retrieve any post type where post meta color value equals to blue OR size meta value equals to XL
获取元数据 "color" 为 "blue" 或者 "size" 为 "XL" 的所有文章类型中的文章

$builder = new Builder();
$wp_query = $builder->createMainMetaQuery("OR")
$wp_query = $builder->createMetaQuery("OR")
->addMetaQuery(MetaQuery::create('color', 'blue'))
->addMetaQuery(MetaQuery::create('size', 'XL'))
->getWPQuery();


Retrieve any post type where post meta price is equal or greater than 10 OR size meta value equals to XL
获取元数据 "price" 大于等于 "10" 并且 "size" 为 "XL" 的所有文章类型中的文章

$builder = new Builder();
$wp_query = $builder->createMainMetaQuery("AND")
$wp_query = $builder->createMetaQuery("AND")
->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC'))
->addMetaQuery(MetaQuery::create('size', 'XL'))
->getWPQuery();
Expand All @@ -38,7 +38,7 @@ Retrieve any post type where post meta price is equal or greater than 10 OR size
Retrieve any post type where post meta price is equal or greater than 10 AND (size meta value equals to XL OR post meta color value equals to blue)

$builder = new Builder();
$builder->createMainMetaQuery("AND")
$builder->createMetaQuery("AND")
->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC'));

$condition = new MetaQueryCollection('OR');
Expand All @@ -49,29 +49,29 @@ Retrieve any post type where post meta price is equal or greater than 10 OR size
$wp_query = $builder->addMetaQueryCollection($condition)
->getWPQuery();

### TAXONOMY QUERIES
### 自定义分类法查询


Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy AND in under 'sweet' in "custom" taxonomy

$builder = new Builder();
$wp_query = $builder->createMainTaxonomyQuery("AND")
$wp_query = $builder->createTaxonomyQuery("AND")
->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools')))
->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet))
->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet')))
->getWPQuery();


Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy BUT exclude contents in their children

$builder = new Builder();
$wp_query = $builder->createMainTaxonomyQuery("AND")
$wp_query = $builder->createTaxonomyQuery("AND")
->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), false))
->getWPQuery();

Retrieve the contents those are NOT under ("pets" OR "tools") values in the "category" taxonomy

$builder = new Builder();
$wp_query = $builder->createMainTaxonomyQuery("AND")
$wp_query = $builder->createTaxonomyQuery("AND")
->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), true, 'NOT IN'))
->getWPQuery();

Expand All @@ -83,23 +83,25 @@ You can have nested relations too
$collection->add(TaxonomyQuery::create('tag', 'slug', array('cats')));
$collection->add(TaxonomyQuery::create('custom', 'slug', array('sweet')));

$wp_query = $builder->createMainTaxonomyQuery("AND")
$wp_query = $builder->createTaxonomyQuery("AND")
->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools')))
->addTaxonomyQueryCollection($collection)
->getWPQuery();


### POST TYPES
### 文章类型

Retrieve all PAGES

$builder = new Builder();
$wp_query = $builder->addPostType(Builder::POST_TYPE_PAGE)->getWPQuery();
$wp_query = $builder->addPostType(Builder::POST_TYPE_PAGE)
->getWPQuery();

Retrieve all CUSTOM POST TYPE

$builder = new Builder();
$wp_query = $builder->addPostType('your_custom')->getWPQuery();
$wp_query = $builder->addPostType('your_custom')
->getWPQuery();

Retrieve all CUSTOM POST TYPE and PAGES

Expand All @@ -109,13 +111,14 @@ Retrieve all CUSTOM POST TYPE and PAGES
->getWPQuery();


### SEARCH
### 搜索

Search contents

$builder = new Builder();

$wp_query = $builder->search("search query")->getWPQuery();
$wp_query = $builder->search("search query")
->getWPQuery();


### IN and NOT IN
Expand All @@ -124,13 +127,15 @@ Retrieve contents with ID in array of IDS

$builder = new Builder();

$wp_query = $builder->inPostIDs(array(1,2,3))->getWPQuery();
$wp_query = $builder->inPostIDs(array(1,2,3))
->getWPQuery();

Retrieve contents with ID not in array of IDS

$builder = new Builder();

$wp_query = $builder->notInPostIDs(array(1,2,3))->getWPQuery();
$wp_query = $builder->notInPostIDs(array(1,2,3))
->getWPQuery();


### ORDERBY
Expand All @@ -139,7 +144,8 @@ Order contents by title descending

$builder = new Builder();

$wp_query = $builder->setOrderBy("title")->getWPQuery();
$wp_query = $builder->setOrderBy("title")
->getWPQuery();


Order contents by date, ascending
Expand All @@ -164,7 +170,8 @@ Order contents by custom meta

$builder = new Builder();

$wp_query = $builder->setOrderByMeta("color", "DESC")->getWPQuery();
$wp_query = $builder->setOrderByMeta("color", "DESC")
->getWPQuery();


Order contents by custom numeric meta
Expand All @@ -181,14 +188,17 @@ Retrieve only 10 contents

$builder = new Builder();

$wp_query = $builder->setLimit(10)->getWPQuery();
$wp_query = $builder->setLimit(10)
->getWPQuery();


Retrieve 20 contents starting from the 10th position

$builder = new Builder();

$wp_query = $builder->setLimit(20)->setOffset(10)->getWPQuery();
$wp_query = $builder->setLimit(20)
->setOffset(10)
->getWPQuery();

### RETRIEVING

Expand Down Expand Up @@ -225,4 +235,7 @@ Get an array containing only the post IDs. This is useful when you want to retur

$builder = new Builder();

$wp_query = $builder->inPostIDs($ids)->getWPQuery();
$wp_query = $builder->inPostIDs($ids)
->getWPQuery();


12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"name": "simettric/wp-query-builder",
"name": "wenprise/wp-query-builder",
"description": "A query builder for WordPress WP_Query, inspired by Doctrine Query Builder",
"keywords": [
"wordpress"
],
"homepage": "http://simettric.com",
"homepage": "https://www.wpzhiku.com",
"license": "MIT",
"authors": [
{
"name": "Asier Marqués",
"email": "asier@simettric.com"
},
{
"name": "Amos Lee",
"email": "amos@wpcio.com"
}
],
"require": {
Expand All @@ -20,12 +24,12 @@
},
"autoload": {
"psr-4": {
"Simettric\\WPQueryBuilder\\": "src/"
"Wenprise\\WPQueryBuilder\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Simettric\\WPQueryBuilder\\Test\\": "tests/"
"Wenprise\\WPQueryBuilder\\Test\\": "tests/"
}
},
"minimum-stability": "dev"
Expand Down
Loading