WordPress.org

Make WordPress Core

Opened 9 months ago

Closed 4 months ago

#46420 closed defect (bug) (fixed)

Warnings on front-end when trying to display the archive for a non-existent term of a custom taxonomy

Reported by: zaantar Owned by: SergeyBiryukov
Milestone: 5.3 Priority: normal
Severity: minor Version: 5.1
Component: Taxonomy Keywords: good-first-bug has-patch needs-testing dev-feedback
Focuses: template Cc:
PR Number:

Description

Steps to reproduce the issue:

  1. Clean site with a Twenty Seventeen theme (but it happens with other themes as well)
  2. Disable permalinks.
  3. Register a custom taxonomy (for example, "kind") and create a term ("k1").
  4. Open this malformed URL in the browser (replace "localhost" if appropriate, of course): http://localhost/?kind=,k1 - note that the term must be valid in order for the issue to occur.
  5. Observe the warning:
Notice: Trying to get property of non-object in /var/www/html/wp-includes/general-template.php on line 2795
Call Stack
#	Time	Memory	Function	Location
1	0.0140	363912	{main}( )	.../index.php:0
2	0.0862	365080	require( '/var/www/html/wp-blog-header.php' )	.../index.php:17
3	1.0117	5400536	require_once( '/var/www/html/wp-includes/template-loader.php' )	.../wp-blog-header.php:19
4	1.0221	5485576	include( '/var/www/html/wp-content/themes/twentyseventeen/archive.php' )	.../template-loader.php:77
5	1.0221	5485576	get_header( ??? )	.../archive.php:13
6	1.0221	5485952	locate_template( ???, ???, ??? )	.../general-template.php:41
7	1.0227	5486048	load_template( ???, ??? )	.../template.php:653
8	1.0234	5486480	require_once( '/var/www/html/wp-content/themes/twentyseventeen/header.php' )	.../template.php:704
9	1.0263	5495688	wp_head( )	.../header.php:22
10	1.0263	5495688	do_action( ???, ??? )	.../general-template.php:2668
11	1.0264	5496064	WP_Hook->do_action( ??? )	.../plugin.php:465
12	1.0264	5496064	WP_Hook->apply_filters( ???, ??? )	.../class-wp-hook.php:310
13	1.0673	5829504	feed_links_extra( ??? )	.../class-wp-hook.php:286

The problem is caused by these two lines in wp-includes/general-template.php:

$term  = get_queried_object();
$tax   = get_taxonomy( $term->taxonomy );

In this case, get_queried_object() returns null and the code is not ready for that.

Attachments (2)

46420.diff (938 bytes) - added by truchot 7 months ago.
46420.2.diff (1.8 KB) - added by donmhico 4 months ago.
Refresh the patch and apply the same solution on get_the_archive_title().

Download all attachments as: .zip

Change History (11)

#1 @joyously
9 months ago

  • Component changed from Themes to Taxonomy

#2 @SergeyBiryukov
7 months ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 5.3

Related: #29660

Fixed in [25310] for the is_tag() check in feed_links_extra(), but not for is_tax(), which was introduced later in [36671].

The is_tax() check should have an if ( $term ) check as well.

@truchot
7 months ago

#3 follow-up: @truchot
7 months ago

  • Keywords has-patch needs-testing added; needs-patch removed

It seems to fix your case.

Needs testing :)

#4 in reply to: ↑ 3 @zaantar
7 months ago

Replying to truchot:

It seems to fix your case.

Needs testing :)

The particular issue is gone but I am seeing more notices caused by the problem in a different place:

Notice: Trying to get property of non-object in /var/www/html/wp-includes/general-template.php on line 1599

Also, the same thing happens in the Twenty Nineteen theme:

Notice: Trying to get property of non-object in /var/www/html/wp-content/themes/twentynineteen/inc/template-functions.php on line 90

Do you want me to open separate tickets for these?

#5 @zaantar
7 months ago

  • Keywords dev-feedback added

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


7 months ago

#7 @SergeyBiryukov
7 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

@donmhico
4 months ago

Refresh the patch and apply the same solution on get_the_archive_title().

#8 @donmhico
4 months ago

On my attachment, 46420.2.diff, line 1607. You'll see

$title = __( 'Not found' );

I'm not sure if this is the most appropriate $title in this case. Any feedback would be greatly appreciated.

Last edited 4 months ago by donmhico (previous) (diff)

#9 @SergeyBiryukov
4 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 45817:

Taxonomy: In feed_links_extra() and get_the_archive_title(), make sure the queried taxonomy term exists before accessing its properties.

Props donmhico, truchot, zaantar.
Fixes #46420.

Note: See TracTickets for help on using tickets.