Make WordPress Core

Opened 9 years ago

Last modified 7 months ago

#37530 new defect (bug)

is_front_page() is based on wrong data -> gives wrong results

Reported by: theinfinity's profile TheInfinity Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.5.3
Component: Query Keywords: has-patch needs-unit-tests needs-test-info reporter-feedback
Focuses: Cc:

Description

1) is_front_page() is based on the SQL query, so it always gives back false when you try to use it eg at pre_get_posts. If it has no way to figure it out if it's the front page or not, it should return null instead
2) is_front_page() in themes gives the wrong result false back when you set an static page for the front page and then modify the main query on start page to posts or any other custom post type except page. This forces you to make an additionally SQL query instead of using the main query to build a custom front page.
Both is because of this: https://core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/query.php#L4458 . This does not really make sense to get the front page.

Attachments (1)

query.patch (514 bytes) - added by dots 9 years ago.
Now is_front_page() function is working when you set an static page for the front page and then modify the main query on start page to posts or any other custom post type except page.

Download all attachments as: .zip

Change History (11)

@dots
9 years ago

Now is_front_page() function is working when you set an static page for the front page and then modify the main query on start page to posts or any other custom post type except page.

#1 @dots
9 years ago

  • Keywords has-patch added

#2 @dots
9 years ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core by sagarmultidots. View the logs.


9 years ago

#4 @swissspidy
9 years ago

  • Keywords needs-unit-tests added

#5 @johnbillion
9 years ago

  • Component changed from General to Query

Thanks for the patch, @dots. This will need unit tests that demonstrate the bug and demonstrate that the fix works as expected.

This ticket was mentioned in Slack in #core by johnbillion. View the logs.


9 years ago

This ticket was mentioned in Slack in #core by mdchiragpatel. View the logs.


9 years ago

#8 @desrosj
6 years ago

  • Milestone set to Future Release

@dots are you able to work on adding unit tests? Here is some documentation to help get started: https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/.

#9 @SirLouen
7 months ago

  • Keywords needs-test-info reporter-feedback added

Combined Reproduction and Patch Test Report

Description

❌ This report can't validate that the patch is working as expected

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty 2.9
  • MU Plugins: None activated
  • Plugins:
    • Front Page Query Checker 1.0.0
    • Test Reports 1.2.0

Reproduction Steps

  1. Get the code in artifacts, add it to a plugin, functions.php or wherever it can be executed
  2. Add a Page
  3. Set that page as Front Page in Settings Reading
  4. Go to the homepage
  5. Check the server error log
  6. 🐞 No error pops in the log, so theoretically the conditions are not being targeted according to the reporter.

Expected Results

  • With the given code, theoretically, an error should appear in log as the condition is being fulfilled?

Actual Results with the Patch

  1. ❌ Error is still not popping in the log.

This is what I'm getting on debug for that condition introduced. Two out of 3 conditions are going to fail:
https://i.imgur.com/dz59Ivg.png

Additional Notes

  • I'm not 100% convinced if I'm looking at the right, place, this is why I'm providing precise steps, so either reporter-feedbackfor reporter @TheInfinity is required, or needs-test-infofor the patch creator @dots could confirm, or tell me where I'm wrong.
  • Not 100% sure but I think what needs some revisiting could be WP_Query::is_page
  • I think this is mostly what other people have been suggesting with "unit tests". Asking for unit tests has been historically the elegant way of asking for a Testing Use Case

Supplemental Artifacts

Test Code

function front_page_query_modifier($query) {
    if ($query->is_main_query() && $query->is_front_page() ) {
        error_log('Front page is being targeted');
    }
    return $query;
}
add_action('pre_get_posts', 'front_page_query_modifier');

#10 @SirLouen
7 months ago

  • Keywords needs-testing removed
Note: See TracTickets for help on using tickets.