Query - Page-Carbajal/WPExpress GitHub Wiki
Query is an abstraction layer for WP_Query.
Query makes it easier to interact with WP_Query. It also makes code much more readable.
Use Cases
A little less conversation a little more action.
Get all posts
<?php
use WPExpress\Query;
$allPosts = Query::Post()->all()->get();
$allCustomPosts = Query::Custom('book')->all()->get();
Get 10 posts
<?php
use WPExpress\Query;
// Note: If limit is not set defaults to 10 using ***showposts*** parameter
$tenPosts = Query::Post()->limit(10)->get();
$tenBooks = Query::Custom('book')->limit(10)->get();
Get posts by Meta Value (Custom Field)
<?php
use WPExpress\Query;
$tenPosts = Query::Post()->meta('fieldName', 'value')->get();
$tenBooks = Query::Custom('book')->meta('fieldName', 'value')->get();
//Note: You can also use and array as value
$somePosts = Query::Post()->meta('fieldName', array( 'value', 'value2' ) )->all()->get();
Get posts without Meta Value
<?php
use WPExpress\Query;
// The method meta takes a third operator indicating the comparison to be used. In this case **NOT**
$somePosts = Query::Post()->meta('fieldName', array( 'value', 'value2' ), 'not' )->all()->get();
$someBooks = Query::Custom('book')->meta('fieldName', 'value', 'not' )->all()->get();
Get posts by Taxonomy
<?php
use WPExpress\Query;
$tenPosts = Query::Post()->term('taxonomy', 'value')->get();
$tenBooks = Query::Custom('book')->term('taxonomy', 'value')->get();
Insert, Delete, Save
<?php
use WPExpress\Query;
// TODO: Add code here
Methods
Ok, that was fun. Now, lets explain the methods a little.
all and limit
all and limit indicate how many posts should WordPress retrieve from the DataBase. As you can expect all indicates every post should be retrieved, while limit indicates the number of posts to be retrieved.
<?php
use WPExpress\Query;
$allPosts = Query::Post()->all()->get();
$fivePosts = Query::Post()->limit(5)->get();
meta
<?php
use WPExpress\Query;
// TODO: Add code here
term
<?php
use WPExpress\Query;
// TODO: Add code here
get, get first, get last
<?php
use WPExpress\Query;
// TODO: Add code here
transient
Under Development
insert, save, delete
Under Development
order and orderBy
Under Development