get_posts inside cron

时间:2012-10-03 作者:Shane

Edited to rewrite my question

我会尽量说清楚,因为我需要尽快解决这个问题。

我禁用了WordPress的cron,并在我的服务器上添加了一个真正的cron作业来调用wp cron。php每天。但是,我调用的函数不起作用。

代码执行似乎停止于get_posts, 如果我尝试使用WP_Query 班没有错误消息,之后的任何代码都不会执行。

当我不添加时numberposts, 但它确实给了我5篇文章,这是numberposts的默认值。

示例:

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

More Info

<帖子类型和分类是在我的插件文件中创建的init 挂钩wp_ajax_nopriv, 和呼叫admin-ajax.php?action=the-action 来自我的服务器cron,但结果完全相同

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 个回复
最合适的回答,由SO网友:Otto 整理而成

无论何时您获得\\u posts或WP\\u Query或类似的内容,都必须记住,代码实际上是一次获得所有post,并将它们从数据库加载到内存中。如果内存不足,那么进程将因错误而终止。试图一次获得大量帖子会经常导致这种情况。

打开PHP的display\\u errors。您可能会看到内存不足错误。

接下来,将PHP memory\\u limit设置提高到128M或256M,看看现在是否有足够的内存来执行此操作。

或者,重写get\\u帖子以使用分页。一次给他们100个,或者沿着这些路线。那么你就不会试图一次将1000篇帖子全部存入内存。

结束