From f84596420abc5ecdf98c55f5f6c224095ac1ab0a Mon Sep 17 00:00:00 2001 From: tellyworth Date: Wed, 8 Dec 2021 16:35:58 +1100 Subject: [PATCH] Add a hasThumbnail pseudo-attribute to the wp:query block This lets us use `wp:query {"query":{"hasThumbnail":true}}` to query only for posts with a featured image/thumbnail. I'm in two minds about the method of doing this; what do you think? --- .../front-page/people-of-wordpress.html | 2 +- .../themes/wporg-news-2021/functions.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/source/wp-content/themes/wporg-news-2021/block-template-parts/front-page/people-of-wordpress.html b/source/wp-content/themes/wporg-news-2021/block-template-parts/front-page/people-of-wordpress.html index 3fd80b80..2b401f1c 100644 --- a/source/wp-content/themes/wporg-news-2021/block-template-parts/front-page/people-of-wordpress.html +++ b/source/wp-content/themes/wporg-news-2021/block-template-parts/front-page/people-of-wordpress.html @@ -1,4 +1,4 @@ - +

People of WordPress

diff --git a/source/wp-content/themes/wporg-news-2021/functions.php b/source/wp-content/themes/wporg-news-2021/functions.php index a6ca2f88..4508c8bb 100644 --- a/source/wp-content/themes/wporg-news-2021/functions.php +++ b/source/wp-content/themes/wporg-news-2021/functions.php @@ -19,6 +19,7 @@ add_filter( 'post_class', __NAMESPACE__ . '\specify_post_classes', 10, 3 ); add_filter( 'theme_file_path', __NAMESPACE__ . '\conditional_template_part', 10, 2 ); add_filter( 'render_block_data', __NAMESPACE__ . '\custom_query_block_attributes' ); +add_action( 'pre_get_posts', __NAMESPACE__ . '\custom_query_parameters' ); add_filter( 'template_redirect', __NAMESPACE__ . '\jetpack_likes_workaround' ); /** @@ -324,11 +325,42 @@ function custom_query_block_attributes( $parsed_block ) { $parsed_block[ 'attrs' ][ 'query' ][ 'categoryIds' ] = [ $category->term_id ]; } } + + if ( isset( $parsed_block[ 'attrs' ][ 'query' ][ 'hasThumbnail' ] ) ) { + // Use the search parameter as a hack for passing through extra context to the custom_query_parameters filter. + // There's probably a better way to do this in general. Ideally the query block would support a hasThumbnail parameter natively. + $parsed_block[ 'attrs' ][ 'query' ][ 'search' ] .= ':query-has-thumbnail'; + } } return $parsed_block; } +/** + * Support additional pseudo-parameters passed to WP_Query from the wp:query block via custom_query_block_attributes. + * + * This is the WP_Query side of the code that lets us create extra parameters needed in some templates. + * + * @param array $query The WP_Query object. + * + * @return array + */ +function custom_query_parameters( $query ) { + // See https://wordpress.stackexchange.com/a/392915 for the idea + if ( $query->is_search() && false !== strpos( $query->get( 's' ), ':query-has-thumbnail' ) ) { + // Add a meta query to only include posts with thumbnails/featured images. + $query->set( 'meta_query', array( + array( + 'key' => '_thumbnail_id', + 'compare' => 'EXISTS', + ) + ) ); + + // Remove the special keyword from the search parameter. + $query->set( 's', str_replace( ':query-has-thumbnail', '', $query->get( 's' ) ) ); + } +} + /** * A Workaround to make Jetpack Likes work with FSE themes. *