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

      }

 }

1 Answer
1

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.

Leave a Reply

Your email address will not be published. Required fields are marked *