最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - WP_Query leaking absurd amounts of memory

programmeradmin0浏览0评论

Every time I call WP_Query() in the function below, Wordpress leaks 8 megs of memory. And since I call this function a lot, things get hairy pretty quickly... :( I've tried unsetting the resulting $queryObject as well as periodically calling wp_cache_flush(), but neither seems to have any effect. Any thoughts?

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() is:

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

Every time I call WP_Query() in the function below, Wordpress leaks 8 megs of memory. And since I call this function a lot, things get hairy pretty quickly... :( I've tried unsetting the resulting $queryObject as well as periodically calling wp_cache_flush(), but neither seems to have any effect. Any thoughts?

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() is:

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;
}
Share Improve this question asked Jun 1, 2012 at 3:25 rinogorinogo 1,4412 gold badges18 silver badges27 bronze badges 2
  • 1 Have you tried the DEBUG BAR plugin? – kaiser Commented Jun 1, 2012 at 9:12
  • how many posts is fetched by WP_Query if your case (when 8mb is leaked)? – Eugene Manuilov Commented Jun 3, 2012 at 10:08
Add a comment  | 

3 Answers 3

Reset to default 14

Excellent responses on WP Hackers: http://lists.automattic/pipermail/wp-hackers/2012-June/043213.html

What you're doing with that query, is loading EVERY matching post into memory, including the full post contents. As you can imagine, this is probably quite a lot of items.

You can pass 'fields' => 'ids' into WP_Query to simply return a list of matching post_ids instead, which should reduce the memory (and processing time) significantly:

http://codex.wordpress/Class_Reference/WP_Query#Post_Field_Parameters

Stumbled upon this while researching the memory issue pointed out here.

In this case, you can use get_the_id instead of using buffering to capture the id, and you could narrow the queried fields to only include ids.

Using wp_suspend_cache_addition( true ); at the beginning of the script solved the problem for me.

发布评论

评论列表(0)

  1. 暂无评论