skip to Main Content

Hello everyone and thank you for taking a look.

What i wish to do is load all my products on one page in order to export them in an xml file.
My problem is not with the xml code but with loading all the products part.

My website has 13.419 products.

Here is my code:

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

I don’t get it, what am i doing wrong?

Here is a gif about the result:
https://ibb.co/WvqxY8j

What is the best way to archive what i want to do?

2

Answers


  1. Chosen as BEST ANSWER

    So after the great answer from "Martin Mirchev" i took everything i wanted from the database directly on the plus side i saved a ton of memory, here is my code: (I am posting it so anyone who has the same idea can find it)

    //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>';    
    

    I would love if someone could give me feedback if i am doing something wrong. Thanks


  2. Add following function in your functions.php file in your active theme.

    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)); 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search