WP_Query泄漏荒谬的内存量


10

每当我在下面的函数中调用WP_Query()时,Wordpress就会泄漏8兆的内存。而且,由于我多次调用此函数,所以事情很快就变得很毛茸茸了……:(我尝试取消生成的$ queryObject以及定期调用wp​​_cache_flush(),但似乎都没有任何效果。

function get_post_ids_in_taxonomies($taxonomies, &$terms=array()) {
    $post_ids = array();

    $query = gen_query_get_posts_in_taxonomies($taxonomies, $terms);
    // var_dump($query);

    //Perform the query
    $queryObject = new WP_Query($query); //*****THE 8 MEGABYTES IS LEAKED HERE*****

    //For all posts found...
    if($queryObject->have_posts()) {
        while($queryObject->have_posts()) {
            $queryObject->the_post();

            //Get the $post_id by capturing the output of the_ID()
            ob_start();
            the_ID();
            $post_id = (int) ob_get_contents();
            ob_end_clean();

            // echo $post_id."\n";
            $post_ids[] = $post_id;
        }
    }

    unset($queryObject);

    return $post_ids;
}

gen_query_get_posts_in_taxonomies()是:

function gen_query_get_posts_in_taxonomies($taxonomies, &$terms=array()) {
    //General query params
    $query = array(
        'posts_per_page'    => -1,  //Get all posts (no paging)
        'tax_query'             => array('relation' => 'OR'),
    );

    //Add the specific taxonomies and terms onto $query['tax_query']
    foreach($taxonomies as $tax) {
        //Get terms in the taxonomies if we haven't yet
        if(!array_key_exists($tax, $terms)) {
            $terms[$tax] = array();

            $terms_tmp = get_terms($tax);
            foreach($terms_tmp as $tt)
                $terms[$tax][] = $tt->term_taxonomy_id;
        }

        $query['tax_query'][] = array(
            'taxonomy' => $tax,
            'terms' => $terms[$tax],
            'field' => 'term_taxonomy_id',
        );
    }

    return $query;
}

1
您是否尝试过DEBUG BAR插件?
kaiser 2012年

WP_Query如果您的情况下(泄漏8mb时)获取多少个帖子?
尤金·马努洛夫

Answers:



3

在研究这里指出的内存问题时偶然发现了这一点。

在这种情况下,可以使用get_the_id而不是使用缓冲来捕获ID,并且可以缩小查询字段的范围,使其仅包含ID。


感谢您的回复,托马斯!我记得,我最终还是写了一些原始的SQL。但是,这可能也会起作用。非常感谢!:)
rinogo
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.