1

I want to loop through all my posts who are called 'products'. For that I use this one:

$products_IDs = new WP_Query( array(
        'post_type' => 'product'
    ));

while ($products_IDs->have_posts() ) : $products_IDs->the_post();
        $y = $y + 1; 
        $myfile = fopen("wp_all_import.txt", "a");
        fwrite($myfile, "Counter: " . $y . "\t");
        fclose($myfile);

        //some code...

        $sql = "INSERT INTO `wp_all_import` (pid, price, price_old, link, shop) VALUES ('$pid', '$price', '$price_old', '$link', '$shop')";
        if($database->query($sql) === TRUE){
            $myfile = fopen("wp_all_import.txt", "a");
            fwrite($myfile, "Inserted: " . $pid . " \n");
            fclose($myfile);
        }

But I always get only 10 loops nevertheless I have more then 10 product pages... More over 100... But the loop always is stopping at 10... look https://gamekeys-shop.de/wp-admin/wp_all_import.txt

I don't know why... But how can I fix that or how can I find out why the loop is always stopping at 10?

Greetings and Thank You!

  • 1
    Set WP_Query parameter posts_per_page to -1. – Nicolai Mar 1 '17 at 21:40
  • What does that mean and how do I do that? - And where? – Jan Mar 1 '17 at 21:41
2

Take a look at WordPress codex for WP_Query for more information.

You'll see there are different parameters for WP_Query, you just need to add the posts_per_page parameter with the value set to -1 to show all posts.

$products_IDs = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => -1,
    ));

while ($products_IDs->have_posts() ) : $products_IDs->the_post();
    // Your code
endwhile;
  • I will test it tomorrow. So it is set to 10 without any parameter? - Does -1 also works if I only want all sites who are existing now? - I do not want allllll sites. Only those product pages who are existing right now! – Jan Mar 1 '17 at 21:53
  • And what does that mean? - "number of post to show per page"? I'm a German native speaker. I don't understand this term. Isn't post and page the same? – Jan Mar 1 '17 at 21:57
  • To simple it, I will explain it as : posts_per_page is like how many posts the $products_ID->have_posts() can contain. For example, if you set it to 10, you'll get 10 posts on yoursite.com/ then you must go to yoursite.com/page/2 to get the 10 next posts. It's a pagination system, not a page post_type. – Elex Mar 1 '17 at 22:02

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.