{"id":19011874,"date":"2026-09-04T16:05:30","date_gmt":"2026-09-04T16:05:30","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/php-warning-undefined-array-key-post_type-in-class-backend-php\/"},"modified":"2026-09-04T16:05:30","modified_gmt":"2026-09-04T16:05:30","slug":"php-warning-undefined-array-key-post_type-in-class-backend-php","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/php-warning-undefined-array-key-post_type-in-class-backend-php\/","title":{"rendered":"PHP Warning: Undefined array key &#8220;post_type&#8221; in class-backend.php"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Environment:<\/strong><\/p>\n\n\n\n<ul>\n<li>WordPress 7.1<\/li>\n\n\n\n<li>PHP 8.3.33<\/li>\n\n\n\n<li>WooCommerce 11.0.1<\/li>\n\n\n\n<li>Server: Apache<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Description:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Getting the following warning in the logs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PHP Warning: Undefined array key \"post_type\" in &#091;site]\/wp-content\/plugins\/search-exclude\/lib\/controllers\/class-backend.php on line 173<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Cause:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Line 173 reads <code>$_REQUEST['post_type']<\/code> directly without checking whether the key exists first:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function save_post_ids_to_search_exclude( $post_ids, $exclude ) {\n    $post_type = $_REQUEST&#091;'post_type'];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On PHP 8.0+, accessing an undefined array key raises a warning instead of silently returning <code>NULL<\/code> (as it did on PHP 7.x). Since <code>save_post_ids_to_search_exclude()<\/code> is presumably being called (directly or via a hook) in a request that doesn&#8217;t include a <code>post_type<\/code> parameter, this fires every time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Suggested fix:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>isset()<\/code> with the null coalescing operator so it degrades gracefully when <code>post_type<\/code> isn&#8217;t present:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$post_type = isset( $_REQUEST&#091;'post_type'] ) ? sanitize_key( $_REQUEST&#091;'post_type'] ) : '';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(Adding <code>sanitize_key()<\/code> here too, since this is unsanitized <code>$_REQUEST<\/code> input being used as an array key shortly after.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Impact:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cosmetic\/log-noise only in my case so far \u2014 doesn&#8217;t appear to break functionality \u2014 but it&#8217;s worth fixing since PHP is trending toward stricter handling of undefined array access in future versions, and it clutters error logs on sites with logging\/monitoring enabled.<\/p>\n","protected":false},"template":"","class_list":["post-19011874","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19011874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19011874\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=19011874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}