Edit File by line
/home/barbar84/www/wp-conte.../themes/Divi/core/componen.../post
File: Query.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class ET_Core_Post_Query {
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* @var ET_Core_Data_Utils
[5] Fix | Delete
*/
[6] Fix | Delete
protected static $_;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Whether or not to negate the next query arg that is set. Default 'false'.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 3.0.99
[12] Fix | Delete
* @var bool
[13] Fix | Delete
*/
[14] Fix | Delete
protected $_negate = false;
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* The query result.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 3.0.99
[20] Fix | Delete
* @var WP_Post|WP_Post[]
[21] Fix | Delete
*/
[22] Fix | Delete
protected $_query_result;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* The args that will be passed to {@see WP_Query} the next time {@see self::run()} is called.
[26] Fix | Delete
*
[27] Fix | Delete
* @since 3.0.99
[28] Fix | Delete
* @var array
[29] Fix | Delete
*/
[30] Fix | Delete
protected $_wp_query_args;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The name of the primary category-style taxonomy for this post type.
[34] Fix | Delete
*
[35] Fix | Delete
* @since 3.0.99
[36] Fix | Delete
* @var string
[37] Fix | Delete
*/
[38] Fix | Delete
public $category_tax;
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* The post type (slug) for this instance.
[42] Fix | Delete
*
[43] Fix | Delete
* @since 3.0.99
[44] Fix | Delete
* @var string
[45] Fix | Delete
*/
[46] Fix | Delete
public $post_type;
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* The name of the primary tag-style taxonomy for this post type.
[50] Fix | Delete
*
[51] Fix | Delete
* @since 3.0.99
[52] Fix | Delete
* @var string
[53] Fix | Delete
*/
[54] Fix | Delete
public $tag_tax;
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* ET_Core_Post_Query constructor.
[58] Fix | Delete
*
[59] Fix | Delete
* @since 3.0.99
[60] Fix | Delete
*
[61] Fix | Delete
* @param string $post_type See {@see self::$post_type}
[62] Fix | Delete
* @param string $category_tax See {@see self::$category_tax}
[63] Fix | Delete
* @param string $tag_tax See {@see self::$tag_tax}
[64] Fix | Delete
*/
[65] Fix | Delete
public function __construct( $post_type = '', $category_tax = '', $tag_tax = '' ) {
[66] Fix | Delete
$this->post_type = $this->post_type ? $this->post_type : $post_type;
[67] Fix | Delete
$this->category_tax = $this->category_tax ? $this->category_tax : $category_tax;
[68] Fix | Delete
$this->tag_tax = $this->tag_tax ? $this->tag_tax : $tag_tax;
[69] Fix | Delete
[70] Fix | Delete
$this->_wp_query_args = array(
[71] Fix | Delete
'post_type' => $this->post_type,
[72] Fix | Delete
'posts_per_page' => -1,
[73] Fix | Delete
);
[74] Fix | Delete
[75] Fix | Delete
if ( ! self::$_ ) {
[76] Fix | Delete
self::$_ = ET_Core_Data_Utils::instance();
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Adds a meta query to the WP Query args for this instance.
[82] Fix | Delete
*
[83] Fix | Delete
* @since 3.0.99
[84] Fix | Delete
*
[85] Fix | Delete
* @param string $key The meta key.
[86] Fix | Delete
* @param string $value The meta value.
[87] Fix | Delete
* @param bool $negate Whether or not to negate this meta query.
[88] Fix | Delete
*/
[89] Fix | Delete
protected function _add_meta_query( $key, $value, $negate ) {
[90] Fix | Delete
if ( ! isset( $this->_wp_query_args['meta_query'] ) ) {
[91] Fix | Delete
$this->_wp_query_args['meta_query'] = array();
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
if ( is_null( $value ) ) {
[95] Fix | Delete
$compare = $negate ? 'NOT EXISTS' : 'EXISTS';
[96] Fix | Delete
} else if ( is_array( $value ) ) {
[97] Fix | Delete
$compare = $negate ? 'NOT IN' : 'IN';
[98] Fix | Delete
} else {
[99] Fix | Delete
$compare = $negate ? '!=' : '=';
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
$query = array(
[103] Fix | Delete
'key' => $key,
[104] Fix | Delete
'compare' => $compare,
[105] Fix | Delete
);
[106] Fix | Delete
[107] Fix | Delete
if ( ! is_null( $value ) ) {
[108] Fix | Delete
$query['value'] = $value;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
if ( '!=' === $compare ) {
[112] Fix | Delete
$query = array(
[113] Fix | Delete
'relation' => 'OR',
[114] Fix | Delete
array(
[115] Fix | Delete
'key' => $key,
[116] Fix | Delete
'compare' => 'NOT EXISTS',
[117] Fix | Delete
),
[118] Fix | Delete
$query,
[119] Fix | Delete
);
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
$this->_wp_query_args['meta_query'][] = $query;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Adds a tax query to the WP Query args for this instance.
[127] Fix | Delete
*
[128] Fix | Delete
* @since 3.0.99
[129] Fix | Delete
*
[130] Fix | Delete
* @param string $taxonomy The taxonomy name.
[131] Fix | Delete
* @param array $terms Taxonomy terms.
[132] Fix | Delete
* @param bool $negate Whether or not to negate this tax query.
[133] Fix | Delete
*/
[134] Fix | Delete
protected function _add_tax_query( $taxonomy, $terms, $negate ) {
[135] Fix | Delete
if ( ! isset( $this->_wp_query_args['tax_query'] ) ) {
[136] Fix | Delete
$this->_wp_query_args['tax_query'] = array();
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
$operator = $negate ? 'NOT IN' : 'IN';
[140] Fix | Delete
$field = is_int( $terms[0] ) ? 'term_id' : 'name';
[141] Fix | Delete
[142] Fix | Delete
$query = array(
[143] Fix | Delete
'taxonomy' => $taxonomy,
[144] Fix | Delete
'field' => $field,
[145] Fix | Delete
'terms' => $terms,
[146] Fix | Delete
'operator' => $operator,
[147] Fix | Delete
);
[148] Fix | Delete
[149] Fix | Delete
if ( $negate ) {
[150] Fix | Delete
$query = array(
[151] Fix | Delete
'relation' => 'OR',
[152] Fix | Delete
array(
[153] Fix | Delete
'taxonomy' => $taxonomy,
[154] Fix | Delete
'operator' => 'NOT EXISTS',
[155] Fix | Delete
),
[156] Fix | Delete
$query,
[157] Fix | Delete
);
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$this->_wp_query_args['tax_query'][] = $query;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Resets {@see self::$_negate} to default then returns the previous value.
[165] Fix | Delete
*
[166] Fix | Delete
* @return bool
[167] Fix | Delete
*/
[168] Fix | Delete
protected function _reset_negate() {
[169] Fix | Delete
$negate = $this->_negate;
[170] Fix | Delete
[171] Fix | Delete
$this->_negate = false;
[172] Fix | Delete
[173] Fix | Delete
return $negate;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Adds a tax query to this instance's WP Query args for it's category taxonomy.
[178] Fix | Delete
*
[179] Fix | Delete
* @since 3.0.99
[180] Fix | Delete
*
[181] Fix | Delete
* @param mixed ...$categories Variable number of category arguments where each arg can be
[182] Fix | Delete
* a single category name or ID or an array of names or IDs.
[183] Fix | Delete
*
[184] Fix | Delete
* @return $this
[185] Fix | Delete
*/
[186] Fix | Delete
public function in_category() {
[187] Fix | Delete
$negate = $this->_reset_negate();
[188] Fix | Delete
[189] Fix | Delete
if ( ! $this->category_tax ) {
[190] Fix | Delete
et_error( 'A category taxonomy has not been set for this query!' );
[191] Fix | Delete
[192] Fix | Delete
return $this;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
$args = func_get_args();
[196] Fix | Delete
$args = self::$_->array_flatten( $args );
[197] Fix | Delete
[198] Fix | Delete
if ( ! $args ) {
[199] Fix | Delete
return $this;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
$this->_add_tax_query( $this->category_tax, $args, $negate );
[203] Fix | Delete
[204] Fix | Delete
return $this;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Negates the next query arg that is set.
[209] Fix | Delete
*
[210] Fix | Delete
* @since 3.0.99
[211] Fix | Delete
*
[212] Fix | Delete
* @return $this
[213] Fix | Delete
*/
[214] Fix | Delete
public function not() {
[215] Fix | Delete
$this->_negate = true;
[216] Fix | Delete
[217] Fix | Delete
return $this;
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Performs a new WP Query using the instance's current query params and then returns the
[222] Fix | Delete
* results. Typically, this method is the last method call in a set of chained calls to other
[223] Fix | Delete
* methods on this class during which various query params are set.
[224] Fix | Delete
*
[225] Fix | Delete
* Examples:
[226] Fix | Delete
*
[227] Fix | Delete
* $cpt_query
[228] Fix | Delete
* ->in_category( 'some_cat' )
[229] Fix | Delete
* ->with_tag( 'some_tag' )
[230] Fix | Delete
* ->run();
[231] Fix | Delete
*
[232] Fix | Delete
* $cpt_query
[233] Fix | Delete
* ->with_tag( 'some_tag' )
[234] Fix | Delete
* ->not()->in_category( 'some_cat' )
[235] Fix | Delete
* ->run();
[236] Fix | Delete
*
[237] Fix | Delete
* @since 3.0.99
[238] Fix | Delete
*
[239] Fix | Delete
* @param array $args Optional. Additional arguments for {@see WP_Query}.
[240] Fix | Delete
*
[241] Fix | Delete
* @return WP_Post|WP_Post[] $posts
[242] Fix | Delete
*/
[243] Fix | Delete
public function run( $args = array() ) {
[244] Fix | Delete
if ( ! is_null( $this->_query_result ) ) {
[245] Fix | Delete
return $this->_query_result;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
$name = $this->post_type;
[249] Fix | Delete
[250] Fix | Delete
if ( $args ) {
[251] Fix | Delete
$this->_wp_query_args = array_merge_recursive( $this->_wp_query_args, $args );
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
/**
[255] Fix | Delete
* Filters the WP Query args for a custom post type query. The dynamic portion of
[256] Fix | Delete
* the filter name, $name, refers to the name of the custom post type.
[257] Fix | Delete
*
[258] Fix | Delete
* @since 3.0.99
[259] Fix | Delete
*
[260] Fix | Delete
* @param array $args {@see WP_Query::__construct()}
[261] Fix | Delete
*/
[262] Fix | Delete
$this->_wp_query_args = apply_filters( "et_core_cpt_{$name}_query_args", $this->_wp_query_args );
[263] Fix | Delete
[264] Fix | Delete
$query = new WP_Query( $this->_wp_query_args );
[265] Fix | Delete
[266] Fix | Delete
$this->_query_result = $query->posts;
[267] Fix | Delete
[268] Fix | Delete
if ( 1 === count( $this->_query_result ) ) {
[269] Fix | Delete
$this->_query_result = array_pop( $this->_query_result );
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return $this->_query_result;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Adds a meta query to this instance's WP Query args.
[277] Fix | Delete
*
[278] Fix | Delete
* @since 3.0.99
[279] Fix | Delete
*
[280] Fix | Delete
* @param string $key The meta key.
[281] Fix | Delete
* @param mixed $value Optional. The meta value to compare. When `$value` is not provided,
[282] Fix | Delete
* the comparison will be 'EXISTS' or 'NOT EXISTS' (when negated).
[283] Fix | Delete
* When `$value` is an array, comparison will be 'IN' or 'NOT IN'.
[284] Fix | Delete
* When `$value` is not an array, comparison will be '=' or '!='.
[285] Fix | Delete
*
[286] Fix | Delete
* @return $this
[287] Fix | Delete
*/
[288] Fix | Delete
public function with_meta( $key, $value = null ) {
[289] Fix | Delete
$this->_add_meta_query( $key, $value, $this->_reset_negate() );
[290] Fix | Delete
[291] Fix | Delete
return $this;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* Adds a tax query to this instance's WP Query args for it's primary tag-like taxonomy.
[296] Fix | Delete
*
[297] Fix | Delete
* @since 3.0.99
[298] Fix | Delete
*
[299] Fix | Delete
* @param mixed ...$tags Variable number of tag arguments where each arg can be
[300] Fix | Delete
* a single tag name or ID, or an array of tag names or IDs.
[301] Fix | Delete
*
[302] Fix | Delete
* @return $this
[303] Fix | Delete
*/
[304] Fix | Delete
public function with_tag() {
[305] Fix | Delete
$negate = $this->_reset_negate();
[306] Fix | Delete
[307] Fix | Delete
if ( ! $this->tag_tax ) {
[308] Fix | Delete
et_error( 'A tag taxonomy has not been set for this query!' );
[309] Fix | Delete
[310] Fix | Delete
return $this;
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
$args = func_get_args();
[314] Fix | Delete
$args = self::$_->array_flatten( $args );
[315] Fix | Delete
[316] Fix | Delete
if ( ! $args ) {
[317] Fix | Delete
return $this;
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
$this->_add_tax_query( $this->tag_tax, $args, $negate );
[321] Fix | Delete
[322] Fix | Delete
return $this;
[323] Fix | Delete
}
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function