首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在一个页面中加载所有产品的Wp_query在50%的页面上停止

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

Stack Overflow用户
提问于 2021-11-28 22:42:33
回答 2查看 54关注 0票数 0

大家好,谢谢你们的收看。

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

我的网站有13.419种产品。

这是我的代码:

代码语言:javascript
运行
复制
//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

我想做什么最好的存档方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-29 09:25:08

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

代码语言:javascript
运行
复制
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)); 
}
票数 0
EN

Stack Overflow用户

发布于 2021-12-01 21:51:53

因此,在“Mirchev”给出了很好的答案之后,我从数据库中直接获取了我想要的所有东西,我节省了大量内存,下面是我的代码:(我正在发布它,以便任何有相同想法的人都能找到它)

代码语言:javascript
运行
复制
//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>';    

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70148099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档