【问题标题】:Why do get_category_by_slug() crashes WordPress 4.8为什么 get_category_by_slug() 会使 WordPress 4.8 崩溃
【发布时间】:2017-08-07 15:05:49
【问题描述】:

我需要从可选择的帖子类别列表中隐藏“未分类”,并编写了一些代码来实现这一点。 此功能在 WP 4.7.5 之前运行良好,类别已成功隐藏:

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;
}

但是,在更高版本(4.8.1 是迄今为止的最新版本)上,每当我尝试编辑帖子或创建新帖子(访问 `localhost/wp-admin/post-new.php")时,我都会得到显示以下错误消息:

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

提高内存限制只会让我出现超时错误。我能够在 WP 4.8 和 4.8.1 的全新安装中复制该行为

看来崩溃是这四行造成的,添加到任何主题的functions.php文件似乎都会导致崩溃:

add_filter('list_terms_exclusions', 'this_will_crash_wordpress');
function this_will_crash_wordpress( $exclude_query ) {
  get_category_by_slug('uncategorized');
}

4.7.5 之间发生了什么变化导致了这个错误?我怎样才能让它消失?我必须重写我的代码吗?如果是这样,如何

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试使用 get_term_by(); https://codex.wordpress.org/Function_Reference/get_term_by

    喜欢这个

    get_term_by('slug', 'uncategorized', 'category');
    

    【讨论】:

    • 提高内存限制只会让我出现超时错误。
    • 尝试使用get_term_by();
    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多