修改Wordpress搜索功能时内存已用尽

时间:2018-10-22 14:49:58

标签: php wordpress advanced-custom-fields

我正在尝试修改wordpress的搜索功能,以禁用通过ACF单选按钮设置为不在站点地图中显示的帖子。

当我导航到该网站时,我收到以下可爱的错误消息:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes) in /Users/***/***/***/***/wp-includes/class-wp-query.php on line 1621

代码:

function exclude_pages_from_search($query) {
    if ( !is_admin() ) {

        $hidePages = new WP_Query( array (
            'meta_key' => 'edit_screen_sitemap',
            'meta_value' => 'hide',
            'fields' => 'ids'
        )); $hidePageIds = implode( ',', $hidePages->posts );
        //print_r($hidePageIds);

        if ( $query->is_main_query() ) {

            if ($query->is_search) {
                $query->set('post__not_in', array($hidePageIds));
            }
        }
    }
}

add_action('pre_get_posts','exclude_pages_from_search');

1 个答案:

答案 0 :(得分:0)

您需要从自己的查询中删除操作。否则,您的函数将递归调用自身,并由于无限递归而耗尽内存。这应该可以解决问题:

myapplication.dev