Memory Issues with get_the_post_thumbnail_url
-
Hi!
I hope someone here can help me, this is driving me nuts… I’m trying to create a custom txt feed for all my woocommerce products. I got arround 6k products in my shop…
This is the code I’m using so far:
function create_product_feed() { try { $count_single = wp_count_posts('product'); $published_products = $count_single->publish; $chunks = ceil($published_products / 500); $file = ABSPATH . 'product-feed.txt'; // generate xml content if ((file_exists( $file ))) { @unlink ( $file ); } $feed = fopen($file, 'a+'); $j = 0; fwrite($feed, chr(239) . chr(187) . chr(191) . 'Bezeichnung|Beschreibung|Preis|Deeplink|Produktbild|Herst_Nr|Hersteller|Lager_Baden|Lager_Moedling|EAN' . "\n"); for($i = 0; $i < $chunks; $i++) { // Construct WP query $wp_query = array( 'post_type' => array('product'), 'category__not_in' => array(46), 'post_status' => 'publish', 'fields' => 'ids', 'offset' => $i * 500, 'posts_per_page' => '500', 'no_found_rows' => true, 'suppress_filters' => false ); $postchunk = new WP_Query($wp_query); $id_array = $postchunk->posts; foreach ($id_array as $post) { $product_data = ''; //$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post), 'single-post-thumbnail'); // sanitize product short description $desc_r = strip_tags(get_the_excerpt($post)); $desc_t = htmlspecialchars(trim($desc_r)); $desc_p = preg_replace("/\r|\n/", "", $desc_t); $desc = strip_tags($desc_p, '<p><a>'); //setup_postdata($post); $product_data .= get_the_title($post) . '|'; $product_data .= $desc . '|'; $product_data .= get_post_meta($post, '_price', true) . '|'; $product_data .= get_permalink($post) . '|'; //$product_data .= $thumb . '|'; $post_id = $post; $product_data .= get_the_post_thumbnail_url($post_id, 'thumbnail') . '|'; $product_data .= get_post_meta($post, '_sku', true) . '|'; $product_data .= get_post_meta($post, 'ean', true) . PHP_EOL; fwrite($feed, chr(239) . chr(187) . chr(191) . $product_data); $j++; unset($product_data); } unset($postchunk); unset($id_array); } // close the file fclose($feed); $response = "Feed erstellt! => " . $published_products . '/' . $chunks . '/' . $j; } catch (exception $ex) { $response = "Fehler: " . $ex; } echo $response; wp_die(); }
The feed gets created to a certain point, BUT I’m getting an internal server error inside google chrome dev console as soon as I use get_the_post_thumbnail_url()…
I set wp_debug and saw that I’m getting a memory error:
[04-Jul-2020 12:35:00 UTC] PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 65536 bytes) in /wp-includes/functions.php on line 4609
and also
[04-Jul-2020 12:35:00 UTC] PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in /wp-includes/meta.php on line 964
When I comment out that piece of code it works fine… I also tried get_post_thumbnail_id() and wp_get_attachment_image_src() but same issue…
It can’t be a memory thing though… before I was trying to create my own feed I was using the plugin “product feed pro” which uses exactly the same functions as I do to get the thumbnail image, without running into any memory issues.
I just don’t know what to do anymore, I’m working on this small function for hours now, without being able to find out what the problem is…
Please help! Thank you for your time!
- The topic ‘Memory Issues with get_the_post_thumbnail_url’ is closed to new replies.