Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Edited to rewrite my question

I'll try to be as clear as possible, as I need to get this resolved quickly.

I disabled WordPress' cron, and added a real cron job on my server to call wp-cron.php every day. The function i'm calling is not working, however.

Code execution seems to stop at get_posts, same thing if I try using the WP_Query class. No error message, and any code after just doesn't execute.

When I don't add numberposts, it does however give me 5 posts, which is the default value for numberposts.

Sample :

 $posts = get_posts( array(
     'post_type'    => 'post-type',
     'numberposts' => -1,
     'tax_query'    => array(
           array(
                'taxonomy'  =>  'tax',
                'field'     =>  'slug',
                'terms' =>  'term-slug'
           )
      )
 ));

More Info

  • The post type and the taxonomy are created in my plugin file on the init hook.
  • The function works fine when being executed from a PHP page, like my plugin's setting page.
  • I tried hooking my function in wp_ajax_nopriv, and calling admin-ajax.php?action=the-action from my server cron, but with the exact same result.
  • I haven't tried keeping WP's cron, because the function takes a long time to execute, and you need a visitor launch it by visiting the site.

Please reply with any questions / code request if more information is required.

Edit - My loop

 set_time_limit( 1800 );

 $stores = get_option( 'stores' );

 foreach( $stores as $store_name => $store )
 {

      $ctime = 0;
      $filename = '';
      $xml_path = '';

      $dir = dir( $store );

      while( false !== ( $entry = $dir->read() ) )
      {

           $info = pathinfo( $entry );

           $ext = strtolower( $info['extension'] );

           $filepath = "{$store}/{$entry}";

           if( is_file( $filepath ) && $ext == 'xml' && filectime( $filepath ) > $ctime )
           {

                $ctime = filectime( $filepath );

                $filename = $entry;

                $xml_path = $filepath;

           }

      }

      if( file_exists( $xml_path ) && is_file( $xml_path ) )
      {

           //
           //Delete products first
           //
           $posts = get_posts( array(
                'post_type' => 'produit-xml',
                'posts_per_page' => -1,
                'tax_query' => array(
                     array(
                          'taxonomy'    =>  'succursale',
                          'field'       =>  'slug',
                          'terms'       =>  $store_name
                     )
                )
           ));

           foreach( $posts as $post )
           {

                $image = get_post_meta( $post->ID, 'nom_photo', true );

                if( $image )
                {

                     $fullpath = ABSPATH . get_option( $store_name . '-image-path' );

                     $fullpath .= '/resized/' . $image;

                     @unlink( $fullpath );

                }

                wp_delete_post( $post->ID, true );

           }

           //
           //Insert operations follow....
           //

      }

 }
share|improve this question
1  
What about when WP_DEBUG is activated? Also can we see your loop? –  Tom J Nowell Oct 9 '12 at 16:11
1  
How many posts are you trying to actually pull out here? Because I bet you're running out of PHP memory when you try to get them all using -1. –  Otto Oct 9 '12 at 16:52
    
@TomJNowell I've added part of my loop, it's quite long so I added the problematic part. I'll try it out with WP_DEBUG. –  Shane Oct 9 '12 at 17:45
    
@Otto I have 13 taxonomy terms, each holding about 1000 to 2000 posts. –  Shane Oct 9 '12 at 17:47
    
Yeah, that would do it. Bump your PHP memory_limit way up, like to 256M or so, and see if it works then. –  Otto Oct 9 '12 at 19:12

1 Answer 1

up vote 4 down vote accepted
+50

Whenever you do get_posts or WP_Query or anything like that, it's important to remember that the code is actually getting all of the Posts at once, and loading them from the database into memory. If you run out of memory, then your process will simply die with an error. Attempting to get very large numbers of posts at once will cause this quite often.

Turn on PHP's display_errors. You'll likely see an out of memory error.

Next, bump up the PHP memory_limit setting to 128M or 256M and see if you now have enough memory to do it.

Alternately, rewrite your get_posts to use pagination. Get them 100 at a time or something along those lines. Then you're not trying to pull 1000s of posts into memory all at once.

share|improve this answer
    
Thanks a bunch, definately getting me on the right track. Increasing my limit hasn't worked, but fetching 500 at a time sure got more done than previously. –  Shane Oct 9 '12 at 20:55
    
I noticed in your Loop that you're only actually using the ID numbers of the posts. Try setting the 'fields' parameter to 'ids'. This will make the query only get the IDs of the posts and not all the rest of the associated content and such that you don't need. Could be loads faster and save a bunch of memory. –  Otto Oct 9 '12 at 21:09
    
You're right, I just found out about the fields parameter, didn't see it in the documentation. Thanks –  Shane Oct 10 '12 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.