3

I have an array $unqiues which contains URL's, I loop over the array with the code below to get the URL titles, insert a new post, update the post meta. Then I change the the posts title and name to the $pid which was returned by wp_insert_post.

This created an endless loop, I have about 600 or fewer URL's in the $unique array. I just hit 75,000 queries within an hour on my host and have to wait an hour for the limit to reset. What caused the endless loop? This code worked when I tested with 10 URL's in the array.

How can this be written more efficiently?

Thanks

foreach($uniques as $unique){
      $pagetitle = getTitle($unique);
      $new_post = array(
            'post_title' => $unique,
            'post_status'   => 'publish',
            'post_type' => 'websites'
        );
        $pid = wp_insert_post($new_post);

        update_post_meta($pid,'title', $pagetitle);
        update_post_meta($pid,'url',$unique);


          $my_post = array(
              'ID'           => $pid,
              'post_title' => $pid,
              'post_name' => $pid
          );

          wp_update_post( $my_post );
}
4
  • How do you run that code? What is the context?
    – s_ha_dum
    Commented Sep 3, 2013 at 1:30
  • @s_ha_dum It fires within a if (isset($_POST['submit']) conditional
    – Anagio
    Commented Sep 3, 2013 at 1:32
  • @Anagio in which file? Is this code wrapped in a function? if so, this function hook into some action?
    – gmazzap
    Commented Sep 3, 2013 at 2:47
  • @G.M. this is outside the loop i've included wp-blog-header.php it's not hooking into any action or filter.
    – Anagio
    Commented Sep 3, 2013 at 2:49

1 Answer 1

3

It sure can be written in a more efficient way (untested):

$added = array();
global $wpdb;
foreach($uniques as $unique){
  $pagetitle = getTitle($unique);
  $new_post = array(
    'post_title' => $unique,
    'post_status' => 'publish',
    'post_type' => 'websites'
  );
  $pid = wp_insert_post($new_post);
  if ($pid) {
    $wpdb->query( $wpdb->prepare(
      "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES
       (%d, '%s', '%s'), (%d, '%s', '%s')",
       $pid, 'title', $pagetitle, $pid, 'url', $unique
    ) );
    $added[] = $pid;
  }
}
if ( ! empty($added) ) {
   $ids = implode(',', $added); 
   $wpdb->query("UPDATE $wpdb->posts SET `post_title` = `ID`, `post_name` = `ID` WHERE `ID` IN ($ids)");
}

With 600 urls this code runs 1200 queries less than your, but maybe is not enough...

6
  • Thanks but why use $wpdb when I have WordPress functions available?
    – Anagio
    Commented Sep 3, 2013 at 3:19
  • 2
    See the last $wpdb query. Do in 1 query what using WP function you do in 600 queries. 599 queries less. The first $wpdb query do in 1 query what you do in 2. 1 query less x 600 urls = 600 queries less. Also consider that WP functions fire filter and action and if you have plugins/theme that hooks in this actions you will run additional (maybe slow) code...
    – gmazzap
    Commented Sep 3, 2013 at 3:22
  • @Anagio I forget to tag you in previous comment. Note also that I have the suspect that some code (plugin/theme) are hooking into save_post action, that is triggered by both wp_insert_post and wp_update_post. If I'm right, you are running that code 1200 times...
    – gmazzap
    Commented Sep 3, 2013 at 3:35
  • I'll reply once I test it, it may not be tonight though tomorrow for sure
    – Anagio
    Commented Sep 3, 2013 at 3:38
  • You are welcome. Have you seen if there are any function hookin 'save_post' action? To speed up loading and exxluding plugin can also try to define 'SHORTINIT' constant before bootstrapping wordpress.
    – gmazzap
    Commented Sep 3, 2013 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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