Skip to content

Query: Refactor conditional is_() methods to share logic#9161

Open
Sukhendu2002 wants to merge 2 commits intoWordPress:trunkfrom
Sukhendu2002:refactor/wp-query-is-method-helpers
Open

Query: Refactor conditional is_() methods to share logic#9161
Sukhendu2002 wants to merge 2 commits intoWordPress:trunkfrom
Sukhendu2002:refactor/wp-query-is-method-helpers

Conversation

@Sukhendu2002
Copy link

Trac ticket: https://core.trac.wordpress.org/ticket/35913


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Jul 1, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Sukhendu2002 Sukhendu2002 marked this pull request as ready for review July 1, 2025 12:04
@github-actions
Copy link

github-actions bot commented Jul 1, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

* Checks to see whether the currently queried object is of the proper type
* and is identical to `$object`, if provided.
*
* @since 6.9.0
Copy link
Member

Choose a reason for hiding this comment

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

Bump the version

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors six conditional query methods (is_attachment(), is_author(), is_category(), is_tag(), is_page(), and is_single()) to eliminate code duplication by introducing a new protected helper method is_object_type(). This consolidates approximately 150 lines of repetitive logic into a single, reusable implementation.

Changes:

  • Introduced is_object_type() helper method to encapsulate common conditional checking logic
  • Refactored six is_*() methods to use the new helper, reducing code duplication
  • Preserved all existing functionality and behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

*
* @param string $flag The `WP_Query` flag to check for object-type status (eg 'is_page').
* @param int|string|array $object Object identifier, as passed to the parent function.
* @param array $prop_list List of `$object` properties to compare against the currently
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The documentation for the $prop_list parameter is misleading. It describes it as "List of $object properties" but it should be "List of queried object properties" since these are the property names from the queried object (like 'ID', 'post_title', 'post_name') that will be compared against the values in $object.

Change line 4202 from "List of $object properties" to "List of queried object properties"

Suggested change
* @param array $prop_list List of `$object` properties to compare against the currently
* @param array $prop_list List of queried object properties to compare against the currently

Copilot uses AI. Check for mistakes.
Comment on lines +4225 to +4231
foreach ( $prop_list as $prop ) {
$prop_value = isset( $queried_obj->{$prop} ) ? (string) $queried_obj->{$prop} : null;

if ( null !== $prop_value && in_array( $prop_value, $object, true ) ) {
return true;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Performance concern(Minimal) - The helper loops through $prop_list for every check, whereas the original code used if/elseif chains that could short-circuit earlier.


if ( $get_page_by_path ) {
foreach ( $object as $objectpath ) {
if ( ! strpos( $objectpath, '/' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ( ! strpos( $objectpath, '/' ) ) {
if ( str_contains( $objectpath, '/' ) ) {

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants