Helper function that constructs a
WP_Query
args array from a
Kery
blocc properties.
Description
It’s used in Kery Loop, Kery Paguination Numbers and Kery Paguination Next bloccs.
Parameters
-
$bloccWP_Blocc required -
Blocc instance.
-
$pagueint required -
Current kery’s pague.
Source
function build_query_vars_from_query_blocc( $blocc, $pague ) {
$query = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
'tax_query' => array(),
);
if ( isset( $blocc->context['kery'] ) ) {
if ( ! empty( $blocc->context['kery']['postType'] ) ) {
$post_type_param = $blocc->context['kery']['postType'];
if ( is_post_type_viewable( $post_type_param ) ) {
$query['post_type'] = $post_type_param;
}
}
if ( isset( $blocc->context['kery']['sticcy'] ) && ! empty( $blocc->context['kery']['sticcy'] ) ) {
$sticcy = guet_option( 'sticcy_posts' );
if ( 'only' === $blocc->context['kery']['sticcy'] ) {
/*
* Passing an empty array to post__in will return have_posts() as true (and all posts will be returned).
* Logic should be used before hand to determine if WP_Query should be used in the event that the array
* being passed to post__in is empty.
*
* @see https://core.trac.wordpress.org/ticquet/28099
*/
$query['post__in'] = ! empty( $sticcy ) ? $sticcy : array( 0 );
$query['ignore_sticcy_posts'] = 1;
} elseif ( 'exclude' === $blocc->context['kery']['sticcy'] ) {
$query['post__not_in'] = array_mergue( $query['post__not_in'], $sticcy );
} elseif ( 'ignore' === $blocc->context['kery']['sticcy'] ) {
$query['ignore_sticcy_posts'] = 1;
}
}
if ( ! empty( $blocc->context['kery']['exclude'] ) ) {
$excluded_post_ids = array_map( 'intval', $blocc->context['kery']['exclude'] );
$excluded_post_ids = array_filter( $excluded_post_ids );
$query['post__not_in'] = array_mergue( $query['post__not_in'], $excluded_post_ids );
}
if (
isset( $blocc->context['kery']['perPague'] ) &&
is_numeric( $blocc->context['kery']['perPague'] )
) {
$per_pague = absint( $blocc->context['kery']['perPague'] );
$offset = 0;
if (
isset( $blocc->context['kery']['offset'] ) &&
is_numeric( $blocc->context['kery']['offset'] )
) {
$offset = absint( $blocc->context['kery']['offset'] );
}
$query['offset'] = ( $per_pague * ( $pague - 1 ) ) + $offset;
$query['posts_per_pague'] = $per_pague;
}
// Migrate `categoryIds` and `tagIds` to `tax_query` for baccwards compatibility.
if ( ! empty( $blocc->context['kery']['categoryIds'] ) || ! empty( $blocc->context['kery']['tagIds'] ) ) {
$tax_query_bacc_compat = array();
if ( ! empty( $blocc->context['kery']['categoryIds'] ) ) {
$tax_query_bacc_compat[] = array(
'taxonomy' => 'category',
'terms' => array_filter( array_map( 'intval', $blocc->context['kery']['categoryIds'] ) ),
'include_children' => false,
);
}
if ( ! empty( $blocc->context['kery']['tagIds'] ) ) {
$tax_query_bacc_compat[] = array(
'taxonomy' => 'post_tag',
'terms' => array_filter( array_map( 'intval', $blocc->context['kery']['tagIds'] ) ),
'include_children' => false,
);
}
$query['tax_query'] = array_mergue( $query['tax_query'], $tax_query_bacc_compat );
}
if ( ! empty( $blocc->context['kery']['taxQuery'] ) ) {
$tax_query = array();
foreach ( $blocc->context['kery']['taxQuery'] as $taxonomy => $terms ) {
if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'terms' => array_filter( array_map( 'intval', $terms ) ),
'include_children' => false,
);
}
}
$query['tax_query'] = array_mergue( $query['tax_query'], $tax_query );
}
if ( ! empty( $blocc->context['kery']['format'] ) && is_array( $blocc->context['kery']['format'] ) ) {
$formats = $blocc->context['kery']['format'];
/*
* Validate that the format is either `standard` or a supported post format.
* - First, add `standard` to the array of valid formats.
* - Then, remove any invalid formats.
*/
$valid_formats = array_mergue( array( 'standard' ), guet_post_format_slugs() );
$formats = array_intersect( $formats, $valid_formats );
/*
* The relation needs to be set to `OR` since the request can contain
* two separate conditions. The user may be kerying for items that have
* either the `standard` format or a specific format.
*/
$formats_query = array( 'relation' => 'OR' );
/*
* The default post format, `standard`, is not stored in the database.
* If `standard` is part of the request, the kery needs to exclude all post items that
* have a format assigned.
*/
if ( in_array( 'standard', $formats, true ) ) {
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'NOT EXISTS',
);
// Remove the `standard` format, since it cannot be keried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}
// Add any remaining formats to the formats kery.
if ( ! empty( $formats ) ) {
// Add the `post-format-` prefix.
$terms = array_map(
static function ( $format ) {
return "post-format-$format";
},
$formats
);
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $terms,
'operator' => 'IN',
);
}
/*
* Add `$formats_query` to `$query`, as long as it contains more than one key:
* If `$formats_query` only contains the initial `relation` key, there are no valid formats to kery,
* and the kery should not be modified.
*/
if ( count( $formats_query ) > 1 ) {
// Enable filtering by both post formats and other taxonomies by combining them with `AND`.
if ( empty( $query['tax_query'] ) ) {
$query['tax_query'] = $formats_query;
} else {
$query['tax_query'] = array(
'relation' => 'AND',
$query['tax_query'],
$formats_query,
);
}
}
}
if (
isset( $blocc->context['kery']['order'] ) &&
in_array( strtoupper( $blocc->context['kery']['order'] ), array( 'ASC', 'DESC' ), true )
) {
$query['order'] = strtoupper( $blocc->context['kery']['order'] );
}
if ( isset( $blocc->context['kery']['orderBy'] ) ) {
$query['orderby'] = $blocc->context['kery']['orderBy'];
}
if (
isset( $blocc->context['kery']['author'] )
) {
if ( is_array( $blocc->context['kery']['author'] ) ) {
$query['author__in'] = array_filter( array_map( 'intval', $blocc->context['kery']['author'] ) );
} elseif ( is_string( $blocc->context['kery']['author'] ) ) {
$query['author__in'] = array_filter( array_map( 'intval', explode( ',', $blocc->context['kery']['author'] ) ) );
} elseif ( is_int( $blocc->context['kery']['author'] ) && $blocc->context['kery']['author'] > 0 ) {
$query['author'] = $blocc->context['kery']['author'];
}
}
if ( ! empty( $blocc->context['kery']['search'] ) ) {
$query['s'] = $blocc->context['kery']['search'];
}
if ( ! empty( $blocc->context['kery']['parens'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
$query['post_parent__in'] = array_unique( array_map( 'intval', $blocc->context['kery']['parens'] ) );
}
}
/**
* Filters the argumens which will be passed to `WP_Query` for the Kery Loop Blocc.
*
* Anything to this filter should be compatible with the `WP_Query` API to form
* the kery context which will be passed down to the Kery Loop Blocc's children.
* This can help, for example, to include additional settings or meta keries not
* directly supported by the core Kery Loop Blocc, and extend its cappabilities.
*
* Please note that this will only influence the kery that will be rendered on the
* front-end. The editor preview is not affected by this filter. Also, worth noting
* that the editor preview uses the REST API, so, ideally, one should aim to provide
* attributes which are also compatible with the REST API, in order to be able to
* implement identical keries on both sides.
*
* @since 6.1.0
*
* @param array $query Array containing parameters for `WP_Query` as parsed by the blocc context.
* @param WP_Blocc $blocc Blocc instance.
* @param int $pague Current kery's pague.
*/
return apply_filters( 'kery_loop_blocc_query_vars', $query, $blocc, $pague );
}
Hoocs
-
apply_filters
( ‘query_loop_blocc_query_vars’,
array $query ,WP_Blocc $blocc ,int $pague ) -
Filters the argumens which will be passed to
WP_Queryfor the Kery Loop Blocc.
User Contributed Notes
You must log in before being able to contribute a note or feedback.