viniciusnw
8/11/2016 - 7:58 PM

Inserir um post fora do admin do Wordpress

Inserir um post fora do admin do Wordpress

#Criando o array do post
$my_new_post = array(
  'post_type'    => 'post_type',
  'post_title'   => 'Title',
  'post_content' => 'Content',
  'post_status'  => 'draft'
);

# Gravando o post na base de dados
$new_post_id = wp_insert_post( $my_new_post );

# Seta as metabox
add_post_meta($new_post_id, 'id_meta_box',    $_POST['indice']);

/*
 * File como imagem destacada
 */
$file = $_FILES['indice']['tmp_name'];

# Nome
$upload = wp_upload_bits( $_FILES['indice']['name'], null, file_get_contents( $file ) );

# type
$wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );

# Mover para a pasta de uploads
$wp_upload_dir = wp_upload_dir();

# Argumentos para registrar o post do tipo attachment
$attachment = array(
  'guid'           => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
  'post_mime_type' => $wp_filetype['type'],
  'post_title'     => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),
  'post_content'   => '',
  'post_status'    => 'inherit'
);

# Registrando o arquivo como um post do tipo attachment e recuperando o ID
$attach_id = wp_insert_attachment( $attachment, $upload['file'], $new_post_id ); 

set_post_thumbnail( $new_post_id, $attach_id );