Skip to content

Commit

Permalink
Query loop / Post template: Enable post format filter (WordPress#64167)
Browse files Browse the repository at this point in the history
Enables filtering the query loop result by post format.

To allow querying a given post format, a new parameter, `format` is added to a new compatibility class called `Gutenberg_REST_Posts_Controller_6_7`, which extends the core class `WP_REST_Posts_Controller`.

Query loop block:
- Adds a new parameter, `format`, to the `query` attribute in block.json.
- Adds a new `FormatControls` that uses a list of supported post formats inside a `FormTokenField`. This control is placed in the `Filters` panel in the block settings sidebar.
- Adds a new utility function `gutenberg_add_format_query_vars_to_query_loop_block` to ensure that the Query loop block can pass the new `format` argument correctly to `WP_Query`. This function is hooked into `query_loop_block_query_vars`.

Post Template block:
The new `format` parameter is passed from the query loop block to the post template block as part of the `query` attribute.

---------

Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: dmsnell <dmsnell@git.wordpress.org>
Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org>
Co-authored-by: nickbohle <nickbohle@git.wordpress.org>
Co-authored-by: SergeyBiryukov <sergeybiryukov@git.wordpress.org>
Co-authored-by: justintadlock <greenshady@git.wordpress.org>
  • Loading branch information
10 people committed Sep 16, 2024
1 parent 9f79666 commit 45d33f0
Show file tree
Hide file tree
Showing 11 changed files with 958 additions and 5 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.7/7314.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7314

* https://github.com/WordPress/gutenberg/pull/64167
60 changes: 60 additions & 0 deletions lib/compat/wordpress-6.7/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,63 @@ function gutenberg_filter_block_type_metadata_settings_allow_variations_php_file
return $settings;
}
add_filter( 'block_type_metadata_settings', 'gutenberg_filter_block_type_metadata_settings_allow_variations_php_file', 10, 2 );

/**
* Adds post format query vars to the query loop block's WP_Query when the block's attributes call for them.
*
* @see 'query_loop_block_query_vars'
*
* @param array $query The query vars.
* @param WP_Block $block Block instance.
* @return array The filtered query vars.
*/
function gutenberg_add_format_query_vars_to_query_loop_block( $query, $block ) {
// Return early if there is no format or if the format is not an array.
if ( empty( $block->context['query']['format'] ) || ! is_array( $block->context['query']['format'] ) ) {
return $query;
}

$formats = $block->context['query']['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.
if ( in_array( 'standard', $formats, true ) ) {
$tax_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(),
'operator' => 'NOT EXISTS',
);
// Remove the standard format, since it cannot be queried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}

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

$tax_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;
}

return $query;
}
add_filter( 'query_loop_block_query_vars', 'gutenberg_add_format_query_vars_to_query_loop_block', 10, 2 );
Loading

0 comments on commit 45d33f0

Please sign in to comment.