-
Notifications
You must be signed in to change notification settings - Fork 43
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
Fix: exclude specific posts from other blocks from query #500
Conversation
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.
For the globals question, you could do something like:
global $all_specific_posts_ids; // Or maybe even a static variable (https://www.tutorialspoint.com/php/php_static_variables.htm).
if ( ! is_array( $all_specific_post_ids ) ) {
// Get all blocks and gather specificPosts ids of all Homepage Articles blocks.
$blocks = parse_blocks( get_the_content() );
$block_name = apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/homepage-articles' );
$all_specific_posts_ids = array_reduce(
$blocks,
function ( $acc, $block ) use ( $block_name ) {
if (
$block_name === $block['blockName'] &&
isset( $block['attrs']['specificMode'] ) &&
isset( $block['attrs']['specificPosts'] ) &&
count( $block['attrs']['specificPosts'] )
) {
return array_merge(
$block['attrs']['specificPosts'],
$acc
);
}
return $acc;
},
[]
);
}
Thanks Claudiu, that works! In: dd93454 |
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.
Looks good and works well 👍
Definitely doesn't need to be revised here, but here's a fun protip for future reference:
isset( $block['attrs']['specificMode'] ) && isset( $block['attrs']['specificPosts'] )
can be simplified as
isset( $block['attrs']['specificMode'], $block['attrs']['specificPosts'] )
You can pass any number of things into isset
and it'll return true
only if they are all set. :)
## [1.7.1](v1.7.0...v1.7.1) (2020-06-09) ### Bug Fixes * always display query controls; disallow choosing 0 posts ([#497](#497)) ([b956111](b956111)) * correct order of arguments in implodes ([0f45074](0f45074)) * don't use main wp_query when rendering homepage posts ([9c804f6](9c804f6)) * reset postdata after restoring wp_query ([d9d01a6](d9d01a6)) * **homepage-posts:** exclude specific posts from other blocks from query ([#500](#500)) ([31181a5](31181a5)), closes [#498](#498) * make bottom margin styles less specific for easier overrides ([#483](#483)) ([93c3aff](93c3aff))
🎉 This PR is included in version 1.7.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
Ideally, creation of
$all_specific_posts_ids
array should happen once per page request (currently it happens once per HP block), but I don't know how to do that – all suggestions welcome!Closes #498
How to test the changes in this Pull Request:
Other information: