class ET_Core_Post_Query {
* @var ET_Core_Data_Utils
* Whether or not to negate the next query arg that is set. Default 'false'.
protected $_negate = false;
protected $_query_result;
* The args that will be passed to {@see WP_Query} the next time {@see self::run()} is called.
protected $_wp_query_args;
* The name of the primary category-style taxonomy for this post type.
* The post type (slug) for this instance.
* The name of the primary tag-style taxonomy for this post type.
* ET_Core_Post_Query constructor.
* @param string $post_type See {@see self::$post_type}
* @param string $category_tax See {@see self::$category_tax}
* @param string $tag_tax See {@see self::$tag_tax}
public function __construct( $post_type = '', $category_tax = '', $tag_tax = '' ) {
$this->post_type = $this->post_type ? $this->post_type : $post_type;
$this->category_tax = $this->category_tax ? $this->category_tax : $category_tax;
$this->tag_tax = $this->tag_tax ? $this->tag_tax : $tag_tax;
$this->_wp_query_args = array(
'post_type' => $this->post_type,
self::$_ = ET_Core_Data_Utils::instance();
* Adds a meta query to the WP Query args for this instance.
* @param string $key The meta key.
* @param string $value The meta value.
* @param bool $negate Whether or not to negate this meta query.
protected function _add_meta_query( $key, $value, $negate ) {
if ( ! isset( $this->_wp_query_args['meta_query'] ) ) {
$this->_wp_query_args['meta_query'] = array();
if ( is_null( $value ) ) {
$compare = $negate ? 'NOT EXISTS' : 'EXISTS';
} else if ( is_array( $value ) ) {
$compare = $negate ? 'NOT IN' : 'IN';
$compare = $negate ? '!=' : '=';
if ( ! is_null( $value ) ) {
$query['value'] = $value;
if ( '!=' === $compare ) {
'compare' => 'NOT EXISTS',
$this->_wp_query_args['meta_query'][] = $query;
* Adds a tax query to the WP Query args for this instance.
* @param string $taxonomy The taxonomy name.
* @param array $terms Taxonomy terms.
* @param bool $negate Whether or not to negate this tax query.
protected function _add_tax_query( $taxonomy, $terms, $negate ) {
if ( ! isset( $this->_wp_query_args['tax_query'] ) ) {
$this->_wp_query_args['tax_query'] = array();
$operator = $negate ? 'NOT IN' : 'IN';
$field = is_int( $terms[0] ) ? 'term_id' : 'name';
'operator' => 'NOT EXISTS',
$this->_wp_query_args['tax_query'][] = $query;
* Resets {@see self::$_negate} to default then returns the previous value.
protected function _reset_negate() {
$negate = $this->_negate;
* Adds a tax query to this instance's WP Query args for it's category taxonomy.
* @param mixed ...$categories Variable number of category arguments where each arg can be
* a single category name or ID or an array of names or IDs.
public function in_category() {
$negate = $this->_reset_negate();
if ( ! $this->category_tax ) {
et_error( 'A category taxonomy has not been set for this query!' );
$args = self::$_->array_flatten( $args );
$this->_add_tax_query( $this->category_tax, $args, $negate );
* Negates the next query arg that is set.
* Performs a new WP Query using the instance's current query params and then returns the
* results. Typically, this method is the last method call in a set of chained calls to other
* methods on this class during which various query params are set.
* ->in_category( 'some_cat' )
* ->with_tag( 'some_tag' )
* ->with_tag( 'some_tag' )
* ->not()->in_category( 'some_cat' )
* @param array $args Optional. Additional arguments for {@see WP_Query}.
* @return WP_Post|WP_Post[] $posts
public function run( $args = array() ) {
if ( ! is_null( $this->_query_result ) ) {
return $this->_query_result;
$name = $this->post_type;
$this->_wp_query_args = array_merge_recursive( $this->_wp_query_args, $args );
* Filters the WP Query args for a custom post type query. The dynamic portion of
* the filter name, $name, refers to the name of the custom post type.
* @param array $args {@see WP_Query::__construct()}
$this->_wp_query_args = apply_filters( "et_core_cpt_{$name}_query_args", $this->_wp_query_args );
$query = new WP_Query( $this->_wp_query_args );
$this->_query_result = $query->posts;
if ( 1 === count( $this->_query_result ) ) {
$this->_query_result = array_pop( $this->_query_result );
return $this->_query_result;
* Adds a meta query to this instance's WP Query args.
* @param string $key The meta key.
* @param mixed $value Optional. The meta value to compare. When `$value` is not provided,
* the comparison will be 'EXISTS' or 'NOT EXISTS' (when negated).
* When `$value` is an array, comparison will be 'IN' or 'NOT IN'.
* When `$value` is not an array, comparison will be '=' or '!='.
public function with_meta( $key, $value = null ) {
$this->_add_meta_query( $key, $value, $this->_reset_negate() );
* Adds a tax query to this instance's WP Query args for it's primary tag-like taxonomy.
* @param mixed ...$tags Variable number of tag arguments where each arg can be
* a single tag name or ID, or an array of tag names or IDs.
public function with_tag() {
$negate = $this->_reset_negate();
if ( ! $this->tag_tax ) {
et_error( 'A tag taxonomy has not been set for this query!' );
$args = self::$_->array_flatten( $args );
$this->_add_tax_query( $this->tag_tax, $args, $negate );