I need to hide "uncategorized" from the list of selectable post categories and wrote some code to achieve that. This function worked fine up to WP 4.7.5, the categories were successfully hidden:
add_filter('list_terms_exclusions', 'myproject_hide_uncategorized',1);
function myproject_hide_uncategorized( $exclude_query ) {
if (function_exists('get_current_screen')) {
$currentScreen = get_current_screen();
if ($currentScreen->base == 'post') {
$newquery = "";
$excluidos = array();
$excluidos[] = get_category_by_slug('sem-categoria')->cat_ID;
$excluidos[] = get_category_by_slug('uncategorized')->cat_ID;
foreach ($excluidos as $excluido) {
$newquery .= " AND t.term_id <> '$excluido' ";
}
$exclude_query .= $newquery;
}
}
return $exclude_query;
}
However, on later versions (4.8.1 being the latest so far), whenever I try to edit a post or create a new one (visiting `localhost/wp-admin/post-new.php"), I get the following error message shown:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 130968 bytes) in /var/www/html/wp-includes/class-wp-term-query.php on line 294
Raising the memory limit just gets me a timeout error. I am able to replicate the behavior in clean installs of both WP 4.8 and 4.8.1
It looks like the crash is caused by these four lines, adding it to the functions.php
file of any theme seems to cause a crash:
add_filter('list_terms_exclusions', 'this_will_crash_wordpress');
function this_will_crash_wordpress( $exclude_query ) {
get_category_by_slug('uncategorized');
}
What changed between 4.7.5 that causes this error? How can I make it go away? Do I have to rewrite my code? If so, how?
return null;
uncategorized