-
Notifications
You must be signed in to change notification settings - Fork 0
Query
Page Carbajal edited this page Mar 3, 2016
·
3 revisions
Query is an abstraction layer for WP_Query.
Query makes it easier to interact with WP_Query. It also makes code much more readable.
A little less conversation a little more action.
<?php
use WPExpress\Query;
$allPosts = Query::Post()->all()->get();
$allCustomPosts = Query::Custom('book')->all()->get();
<?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();
<?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();
<?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();
<?php
use WPExpress\Query;
$tenPosts = Query::Post()->term('taxonomy', 'value')->get();
$tenBooks = Query::Custom('book')->term('taxonomy', 'value')->get();
<?php
use WPExpress\Query;
// TODO: Add code here
Ok, that was fun. Now, lets explain the methods a little.
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();
<?php
use WPExpress\Query;
// TODO: Add code here
<?php
use WPExpress\Query;
// TODO: Add code here
<?php
use WPExpress\Query;
// TODO: Add code here
Under Development
Under Development
Under Development