I am using woocommerce and i have around 1500 product in one category, i have a brand as a custom taxonomy, and on category page i grab all brands based on product id, Here when i pass post_per_page to -1 it shows out of memory issue,

I have increased memory limit to 500M as well but still same.Here is the code

  global $post;
                        global $product;
                        $cate = get_queried_object();
                        $catename = $cate->slug;
                        $category_id = $cate->id;

                        $taxonomy = 'product_cat';
                        $args = array( 'post_type' => 'product', 'posts_per_page' =>-1, 'product_cat' => $catename, 'orderby' => 'ASC','hide_empty'=> 1 );

                        $loop = new WP_Query( $args );

                        while ( $loop->have_posts() ) : $loop->the_post();                     
                        $termbrand[] = wp_get_post_terms($loop->post->ID,'brand')[0]->term_id;

it works when i write like, so it can not process more post and take long time.

$args = array( 'post_type' => 'product', 'posts_per_page' =>1200, 'product_cat' => $catename, 'orderby' => 'ASC','hide_empty'=> 1 );
  • You can use also 'fields' => 'ids' in the arguments to get only the ids. That way you save memory because content etc are not being returned.
    – Laxmana
    May 8 '17 at 15:15

Try - > 'cache_results' => false

$args = array( 'post_type' => 'product', 'posts_per_page' =>-1, 'product_cat' => $catename, 'orderby' => 'ASC','hide_empty'=> 1, 'cache_results' => false );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.