Wp_query 在一个页面中加载所有产品在页面的 50% 处停止

Wp_query loading all products in one page stops at 50% of the pages

大家好,感谢您的观看。

我想做的是将我的所有产品加载到一个页面上,以便将它们导出到 xml 文件中。 我的问题不在于 xml 代码,而在于加载所有产品部分。

我的网站有 13.419 个产品。

这是我的代码:

//Get The Number Of Products
$count_posts = wp_count_posts( 'product' ); //Get all Products
$number_of_posts =  $count_posts->publish; //Get only the published ones

//Divide them into pages
$number_of_pages =  $number_of_posts / 9; //Dividing them by 9 thats its going to be per page
$number_of_pages_rounded =  round($number_of_pages, 0); //Rounding up for the loop

//Create Numbers For showing lines and Pages
$mynumber = 1;
$my_number_2 = 1;

//Just Echoing
echo 'Totla Posts: ' . $number_of_posts . '<br>';
echo 'Total Pages: ' . $number_of_pages_rounded .'<br>';
echo '<br>';
echo "<br>  ======1===== <br><br>";
//Page Loop
for($i == 0; $i < $number_of_pages_rounded; $i++) {
    //Query Arguments
    $args3 = array(
        'posts_per_page' => 9,
        'post_type'   => 'product',
        'post_status' => 'publish',
        'paged' => $my_number_2,
        );
    //Query
    $loop = new WP_Query( $args3 );
    //Product Loop
    if ( $loop->have_posts() ){
        while ( $loop->have_posts() ){
            $loop->the_post();
            global $product;

            echo $mynumber .') ' .$product->id.'<br>';
            echo '-----------'.'<br>';
            $mynumber++;
        }
        //wp_reset_postdata();
    }
    $my_number_2++;
    echo "<br>  ======$my_number_2===== <br><br>";
}

//Just to stop the rest of the site to load
die;

我不明白,我做错了什么?

这是关于结果的 gif: https://ibb.co/WvqxY8j

存档我想做的事情的最佳方式是什么?

在活动主题的 functions.php 文件中添加以下函数。

function grab_all_cpt_ids() {
    global $wpdb;
    // This is more fast method over WP_Query which will do almost the same query
    $results = $wpdb->get_results(
        "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'post_type' AND post_status = 'publish'");
    
    $total_results = count($results); // Just to know how much results we expect.
    $output = array();
    $i=0;
    $output[] .= 'Total results - '.$total_results;
    foreach($results as $result){
        $i++;
        $output[] .= $result->ID; 
        //Break every 9 results
        if($i % 9 == 0) {
            $output[] .= '=============';
        }
    }
    //Run the function anywere you want and enable debug to see results or just print it out
    error_log(print_r($output,true)); 
}

因此,在“Martin Mirchev”给出了很好的回答之后,我直接从数据库中获取了我想要的所有内容,这节省了大量内存,这是我的代码:(我将其发布,所以任何拥有同样的想法可以找到它)

//My site has total Of 13.419 products
//Get The Number Of Products
$count_posts = wp_count_posts( 'product' ); //Get all Products
$number_of_posts =  $count_posts->publish; //Get only the published ones
$count_the_lines = 1;
//Just Echoing
echo 'Totla Products: ' . $number_of_posts . '<br>';
echo 'Memory Limit: ' . ini_get('memory_limit') .'<br>';
echo 'Memory Used At this Point: '.memory_get_usage(true);
echo '<br>';

global $wpdb;
$results = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'");

echo '<br>';
echo count($results);
echo '<br>';
//Just to check the memory
echo 'Memory Used At this Point: '.memory_get_usage(true).'<br>';
foreach($results as $result){

    $sku = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_sku' ");
    $price = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_price' ");
    $stock_status = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_stock' ");
    $regular_price = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_regular_price' ");
    $sale_price = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_sale_price' ");
    $sku = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_sku' ");
    $weight = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_weight' ");
    $image = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $result->ID AND meta_key = '_thumbnail_id' ");

    echo '<br>';
    echo $count_the_lines .') ' . $result->ID ;
    echo '<br>';
    $count_the_lines++;
    echo '<br>';
    echo $sku[0]->meta_value;
    echo '<br>';
    echo $price[0]->meta_value;
    echo '<br>';
    echo $stock_status[0]->meta_value;
    echo '<br>';
    echo  $regular_price[0]->meta_value;
    echo '<br>';
    echo $sale_price[0]->meta_value;
    echo '<br>';
    echo $weight[0]->meta_value;
    echo '<br>';
    echo  $image[0]->meta_value;
    echo '<br>';
    echo '------------------------------------------------<br>';
}
//Just to check the memory
echo 'Memory Used At this Point: '.memory_get_usage(true).'<br>';    

如果我做错了什么,如果有人能给我反馈,我会很高兴。 谢谢