Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

359
Visualizações
Attaching a thumbnail while using wp_insert_post() in WordPress

I have a script that creates posts using wp_insert_post(). Every day the script creates 12 posts, and the posts have the same names.

This is the code used:

require_once( ABSPATH . 'wp-admin/includes/post.php' );

global $post;

    function PostCreator($title, $name, $content, $sign, $meta_input) { 
        //$postID = post_exists( $title );
            $post_data = array(
                'post_title'    => $title,
                'post_content'  => $content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_author'   => '1',
                'post_category' => array('category' => 2),
                'meta_input'    => $meta_input,
                'post_name' => $name,
                );

            wp_insert_post( $post_data, $error_obj );

            if(!isset($post))
                add_action('admin_init', 'hbt_create_post' );
            return $error_obj;
        } 

What I want to do is have each post automatically attach an thumbnail from the media library (not upload a new image every time a post is created)
I have all 12 images in the media library.
My problem is everything I try seems to either not work, or try to re-upload images, which fills my server with duplicates.

Anyone have any insight as to what I might be able to do?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can set the thumbnail for a post with the following function:

set_post_thumbnail( $post, $thumbnail_id );

Take a look at the WordPress codex

So after inserting the post with a slight change to the code you have already, you can then grab the ID of the new post with:

$post_ID = wp_insert_post($post);

Then you can use that to add the image using set_post_thumbnail.

So your code should look something like this overall:

 $post_data = array(
            'post_title'    => $title,
            'post_content'  => $content,
            'post_status'   => 'publish',
            'post_type'     => 'post',
            'post_author'   => '1',
            'post_category' => array('category' => 2),
            'meta_input'    => $meta_input,
            'post_name' => $name,
            );

$post_ID = wp_insert_post( $post_data, $error_obj );

$thumbnail_id = {your image id from media library};
set_post_thumbnail( $post_ID, $thumbnail_id );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda