5 edited tags
| link
4 added 2003 characters in body
source | link
public function create( $posted ) {
    global $ptp_importer;

    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}

The line below creates another 10 posts.

// Create related
$this->create_related( $posted['group'], $post_id, $file_data );

I'm not sure how to split the query since all the files are fetched from the form and created as posts at once so maybe I have to use sleep() as @kaiser suggested. But how much time should I assign to sleep? Will it depend on some php.ini config -- max_execution_time?

public function create( $posted ) {
    global $ptp_importer;

    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}
public function create( $posted ) {
    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}

The line below creates another 10 posts.

// Create related
$this->create_related( $posted['group'], $post_id, $file_data );

I'm not sure how to split the query since all the files are fetched from the form and created as posts at once so maybe I have to use sleep() as @kaiser suggested. But how much time should I assign to sleep? Will it depend on some php.ini config -- max_execution_time?

3 added 2003 characters in body
source | link

EDIT: As requested by @kaiser, here's the function the creates the post for each image:

public function create( $posted ) {
    global $ptp_importer;

    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}

EDIT: As requested by @kaiser, here's the function the creates the post for each image:

public function create( $posted ) {
    global $ptp_importer;

    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}
2 added 94 characters in body
source | link
1
source | link