-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add support for the format
property in query
#7314
Changes from 8 commits
7b34ec9
24efb21
8e44597
d7ab338
a4fb1b4
7eec23a
83eefcb
5fb2c49
5058604
1b974aa
3244361
5ee2750
c71f92f
19ba793
ef44367
47e1581
ad5ea9f
0d3d4dc
4e69a77
6a78361
290a106
8ae0075
53e45f4
54f6b67
16223d6
4aa0b29
f1508b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2330,6 +2330,7 @@ function wp_migrate_old_typography_shape( $metadata ) { | |
* | ||
* @since 5.8.0 | ||
* @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query. | ||
* @since 6.7.0 Added support for the `format` property in query. | ||
* | ||
* @param WP_Block $block Block instance. | ||
* @param int $page Current query's page. | ||
|
@@ -2420,6 +2421,48 @@ function build_query_vars_from_query_block( $block, $page ) { | |
} | ||
} | ||
} | ||
if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) { | ||
$formats = $block->context['query']['format']; | ||
$tax_query = array( 'relation' => 'OR' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My reading of this code is that if someone sets up a query for both formats and taxomomies, this will return posts that contain either. Eg a query for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think something is not working correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an example query loop for listing posts in category 4, with either the gallery or link format. The category id needs to be updated to match a category available on the installation.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have changed it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 In the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am badly regretting not documenting why I added this relation in the first place. Once I get back to work I will continue doing more logging of the different queries: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After more testing and logging of
So that is why the testing earlier showed the same results: I was not testing with a combination of |
||
|
||
// 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. | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( in_array( 'standard', $formats, true ) ) { | ||
$tax_query[] = array( | ||
'taxonomy' => 'post_format', | ||
'field' => 'slug', | ||
'terms' => array(), | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'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 ) ) { | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Add the `post-format-` prefix. | ||
$terms = array_map( | ||
static function ( $format ) { | ||
return 'post-format-' . $format; | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
$formats | ||
); | ||
$tax_query[] = array( | ||
'taxonomy' => 'post_format', | ||
'field' => 'slug', | ||
'terms' => $terms, | ||
'operator' => 'IN', | ||
); | ||
} | ||
if ( ! isset( $query['tax_query'] ) ) { | ||
$query['tax_query'] = array(); | ||
} | ||
// This condition is intended to prevent `$tax_query` from being added to `$query` | ||
// if it only contains the relation. | ||
carolinan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( count( $tax_query ) > 1 ) { | ||
$query['tax_query'][] = $tax_query; | ||
} | ||
} | ||
if ( | ||
isset( $block->context['query']['order'] ) && | ||
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validation: Ensure that the formats come from the supported values only provided by
get_post_format_slugs()
.