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

Add a hasThumbnail pseudo-attribute to the wp:query block #111

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- wp:query {"query":{"perPage":5,"categoryIds":[25]},"tagName":"section","className":"front__people-of-wordpress","align":"full"} -->
<!-- wp:query {"query":{"perPage":5,"categoryIds":[25],"hasThumbnail":true},"tagName":"section","className":"front__people-of-wordpress","align":"full"} -->
<section class="front__people-of-wordpress alignfull">
<!-- wp:heading {"level":2} -->
<h2>People of WordPress</h2>
Expand Down
32 changes: 32 additions & 0 deletions source/wp-content/themes/wporg-news-2021/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

/**
Expand Down Expand Up @@ -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.
*
Expand Down