for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Helper for work with WP_Query.
*
* @package Classy
*/
namespace Classy;
* Class Query_Helper.
class Query_Helper {
* Finds or creates new query based on provided params.
* @param array|boolean $args Args for WP_Query.
* @return \WP_Query
public static function find_query( $args = false ) {
$default_args = array();
if ( ! $args ) {
return self::get_current_query();
}
if ( is_array( $args ) ) {
return new \WP_Query( array_merge( $default_args, $args ) );
return new \WP_Query( $default_args );
* Returns current WP_Query.
public static function get_current_query() {
global $wp_query;
global
Instead of relying on global state, we recommend one of these alternatives:
function myFunction($a, $b) { // Do something }
class MyClass { private $a; private $b; public function __construct($a, $b) { $this->a = $a; $this->b = $b; } public function myFunction() { // Do something } }
$query =& $wp_query;
$query = self::handle_maybe_custom_posts_page( $query );
return $query;
* Checks and returns WP_Query for home posts page.
* @param \WP_Query $query WP_Query object.
private static function handle_maybe_custom_posts_page( $query ) {
if ( $custom_posts_page = get_option( 'page_for_posts' ) ) {
if (
isset( $query->query['p'] ) &&
absint( $query->query['p'] ) === absint( $custom_posts_page )
) {
return new \WP_Query( array( 'post_type' => 'post' ) );
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state