Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport from WordPress core: Improvements for the post format query loop filter #66037

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions lib/compat/wordpress-6.7/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,45 +59,69 @@ function gutenberg_add_format_query_vars_to_query_loop_block( $query, $block ) {
return $query;
}

$formats = $block->context['query']['format'];
$tax_query = array( 'relation' => 'OR' );
$formats = $block->context['query']['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_merge( array( 'standard' ), get_post_format_slugs() );
$formats = array_intersect( $formats, $valid_formats );

// The default post format, 'standard', is not stored in the database.
// If 'standard' is part of the request, the query needs to exclude all post items that
// have a format assigned.
/*
* The relation needs to be set to `OR` since the request can contain
* two separate conditions. The user may be querying 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 query needs to exclude all post items that
* have a format assigned.
*/
if ( in_array( 'standard', $formats, true ) ) {
$tax_query[] = array(
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(),
'operator' => 'NOT EXISTS',
);
// Remove the standard format, since it cannot be queried.
// Remove the `standard` format, since it cannot be queried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}

// Add any remaining formats to the tax query.
// Add any remaining formats to the formats query.
if ( ! empty( $formats ) ) {
// Add the post-format- prefix.
$terms = array_map(
// Add the `post-format-` prefix.
$terms = array_map(
static function ( $format ) {
return 'post-format-' . $format;
return "post-format-$format";
},
$formats
);

$tax_query[] = array(
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $terms,
'operator' => 'IN',
);
}

// This condition is intended to prevent $tax_query from being added to $query
// if it only contains the relation.
if ( count( $tax_query ) > 1 ) {
$query['tax_query'][] = $tax_query;
/*
* 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 query,
* and the query 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,
);
}
}

return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,49 +157,55 @@ public function get_items( $request ) {
$args = $this->prepare_tax_query( $args, $request );

if ( ! empty( $request['format'] ) ) {
$formats = $request['format'];
$tax_query = array( 'relation' => 'OR' );

// The default post format, 'standard', is not stored in the database.
// If 'standard' is part of the request, the query needs to exclude all post items that
// have a format assigned.
$formats = $request['format'];
/*
* The relation needs to be set to `OR` since the request can contain
* two separate conditions. The user may be querying 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 query needs to exclude all post items that
* have a format assigned.
*/
if ( in_array( 'standard', $formats, true ) ) {
$tax_query[] = array(
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(),
'operator' => 'NOT EXISTS',
);
// Remove the standard format, since it cannot be queried.
// Remove the `standard` format, since it cannot be queried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}

// Add any remaining formats to the tax query.
// Add any remaining formats to the formats query.
if ( ! empty( $formats ) ) {
// Add the post-format- prefix.
// Add the `post-format-` prefix.
$terms = array_map(
static function ( $format ) {
return 'post-format-' . $format;
return "post-format-$format";
},
$formats
);

$tax_query[] = array(
$formats_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $terms,
'operator' => 'IN',
);
}

// Enable filtering by both post formats and other taxonomies by combining them with AND.
// Enable filtering by both post formats and other taxonomies by combining them with `AND`.
if ( isset( $args['tax_query'] ) ) {
$args['tax_query'][] = array(
'relation' => 'AND',
$tax_query,
$formats_query,
);
} else {
$args['tax_query'] = $tax_query;
$args['tax_query'] = $formats_query;
}
}

Expand Down
Loading