Aqui está um exemplo de como ignorar essa limitação usando ações / ganchos:
function new_attachment( $att_id ){
// the post this was sideloaded into is the attachments parent!
// fetch the attachment post
$att = get_post( $att_id );
// grab it's parent
$post_id = $att->post_parent;
// set the featured post
set_post_thumbnail( $post_id, $att_id );
}
// add the function above to catch the attachments creation
add_action('add_attachment','new_attachment');
// load the attachment from the URL
media_sideload_image($image_url, $post_id, $post_id);
// we have the image now, and the function above will have fired too setting the thumbnail ID in the process, so lets remove the hook so we don't cause any more trouble
remove_action('add_attachment','new_attachment');
A ideia é que quando media_sideload_image
for executado,
- faz o download da imagem
- adiciona-o como um anexo (uma postagem do tipo
attachment
) - em seguida, anexa esse anexo à postagem cujo ID você forneceu ($ post_id)
Seu problema é que ele não fornece o ID de postagens do anexo recém-criado.
Mas , quando um anexo é criado, uma ação é disparada contendo seu ID. Podemos nos conectar a isso antes de criarmos o anexo e salvar a miniatura em destaque com o ID de postagem que ele nos forneceu e, em seguida, liberar a seguir.