[wp-trac] [WordPress Trac] #59224: get_pages: duplicate `WP_Query::get_posts` call
WordPress Trac
noreply at wordpress.org
Tue Oct 10 09:20:23 UTC 2023
#59224: get_pages: duplicate `WP_Query::get_posts` call
------------------------------------------+--------------------------
Reporter: david.binda | Owner: joemcgill
Type: defect (bug) | Status: closed
Priority: normal | Milestone: 6.3.2
Component: Posts, Post Types | Version: 6.3
Severity: normal | Resolution: fixed
Keywords: has-patch commit fixed-major | Focuses: performance
------------------------------------------+--------------------------
Comment (by dilip2615):
As per your description, it sounds like there is a potential for
optimizing the `get_pages` function to avoid unnecessary database queries.
If you're looking to optimize and make changes to a WordPress Core
function, there are a few general steps you'll want to follow:
1. **Create a Child Theme**: Always create a [child
theme](https://developer.wordpress.org/themes/advanced-topics/child-
themes/) to ensure that updates to the main theme do not erase your
customizations.
2. **Duplicate and Modify**: In your child theme, duplicate the function
that you want to change, renaming it to avoid function naming conflicts.
3. **Make Your Changes**: Edit the new function to optimize it as per your
needs.
4. **Use Your Custom Function**: Replace calls to the original function
with calls to your new, optimized function where needed.
However, considering your question, let's look at a more specific approach
for optimizing the `get_pages` function based on your description:
1. **Use WP_Query Efficiently**: Avoid calling `WP_Query::get_posts()`
twice, by utilizing the posts that are already fetched and available
through the `WP_Query->posts` property.
2. **Modify or Create a Function**: Write a new function or filter that
uses the optimized method of querying posts/pages.
Here's a rough, theoretical example, as a basic reference to give you a
starting point. This is not meant to be a direct solution, but rather a
guideline:
```php
function optimized_get_pages( $args = array() ) {
// ... your extra code/logic ...
$query = new WP_Query( $args );
// No need to call $query->get_posts() as we'll use $query->posts
directly.
// ... possibly more of your code/logic ...
return $query->posts;
}
```
Now you would use `optimized_get_pages()` instead of `get_pages()` in your
theme or plugin, to utilize the more optimized version of the function.
Note: The exact modification would greatly depend on the specific logic
and requirements of your project, so adapt as necessary.
If you believe that this optimization should be part of the WordPress
Core, consider creating a ticket on the [WordPress
Trac](https://core.trac.wordpress.org/), or contribute to the [WordPress
Core](https://make.wordpress.org/core/) directly. Always remember to test
thoroughly to ensure that your changes do not introduce new issues or
break existing functionality.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/59224#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list