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 filter when building WP_Query arguments #1412

Merged
merged 4 commits into from
Jan 5, 2024

Conversation

alexmerz
Copy link
Contributor

@alexmerz alexmerz commented Mar 30, 2023

Allow to customize the arguments for WP_Query created by build_articles_query using the new filter 'newspack_blocks_build_articles_query'.

All Submissions:

Changes proposed in this Pull Request:

Our websites make heavy use of the Homepage Articles block on the Homepage and related type of pages. This results in a significant amount of slow WP_Query calls by the Homepage Articles blocks in case of a cache invalidation caused by new post publications or content changes to these pages. The underlying reason for the slow queries is the default usage of SQL_CALC_FOUND_ROWS by Wordpress. As this seems not to be necessary for a Homepage Articles block, it would be usefull to set the argument 'no_found_rows' for WP_Query in such situations.

But as such a change might break other existing code in the plugin or third party usage, the best way seems to add a filter to build_articles_query() to allow users to add such an argument in a filter when usefull.

I our case we were a able to reduce the time to generate the Homepage from 24 seconds down to 13 seconds by adding the filter and add the 'no_found_rows' argument in the custom filter.

Closes # .

How to test the changes in this Pull Request:

  1. Add a filter such as
add_filter( 'newspack_blocks_build_articles_query', function( $args, $attributes, $block_name) {
	if ( 'newspack-blocks/homepage-articles' === $block_name ) {
		$args['no_found_rows'] = true;
	}
	return $args;
}, 10, 3 );
  1. The resulting SQL should not contain the SQL_CALC_FOUND_ROWS keyword.
  2. Execution time for the SQL statement should improve.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you successfully ran tests with your changes locally?

Allow to customize the arguments for WP_Query created by build_articles_query using the new filter 'newspack_blocks_build_articles_query'.
@alexmerz alexmerz requested a review from a team as a code owner March 30, 2023 09:46
@leogermani
Copy link
Contributor

Thanks @alexmerz ,

Could you please add a doc block to the new filter? Please have a look at the WP coding standards here https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/#4-hooks-actions-and-filters

@alexmerz
Copy link
Contributor Author

alexmerz commented Apr 1, 2023

@leogermani Doc block is added, feel free to adjust the "since" argument.

Copy link
Contributor

@leogermani leogermani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adekbadek adekbadek merged commit f960bd6 into Automattic:master Jan 5, 2024
@miguelpeixe miguelpeixe mentioned this pull request Jan 9, 2024
6 tasks
matticbot pushed a commit that referenced this pull request Jan 11, 2024
# [2.6.0-alpha.1](v2.5.0...v2.6.0-alpha.1) (2024-01-11)

### Features

* add filter when building WP_Query arguments ([#1412](#1412)) ([f960bd6](f960bd6))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 2.6.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Jan 25, 2024
# [2.6.0](v2.5.0...v2.6.0) (2024-01-25)

### Features

* add filter when building WP_Query arguments ([#1412](#1412)) ([f960bd6](f960bd6))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 2.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants