Wordpress: post inseriti programmaticamente non visibili nel profilo dell'autore
18 giu 2022, 14:17:53
Visualizzazioni: 74
Voti: 1
Ho inserito un post programmaticamente in WordPress. Quando visualizzo il post nella pagina dell'autore, il post che ho inserito programmaticamente non viene mostrato, ma se aggiorno manualmente il post, poi funziona e viene visualizzato correttamente. Qual è la causa e come posso risolverlo? Esiste una guida?
Codice nel mio plugin:
add_action('wp_ajax_insert_data_to_post_type_func', 'insert_data_to_post_type_func');
function insert_data_to_post_type_func(){
global $wpdb;
$table_name = $wpdb->prefix . 'ebay_listings';
$select_query = $wpdb->get_results("SELECT * FROM $table_name LIMIT 1");
$user_id_get = $_POST['userID'];
foreach ($select_query as $sq) {
$heading = $sq->title;
$x = json_decode($heading);
$des = $sq->long_desctiption;
$y = json_decode($des);
$z = json_decode($sq->add_id);
$imgs = json_decode($sq->image_URL);
$date = json_decode($sq->date);
$location = json_decode($sq->location);
$list_items = json_decode($sq->list_items);
$price = json_decode($sq->price);
foreach ($x as $tx) {
$title[] = $tx;
}
foreach ($y as $lng_des) {
$description[] = $lng_des;
}
foreach ($z as $add) {
$add_id[] = $add;
}
foreach ($date as $dt) {
$eb_date[] = $dt;
}
foreach ($location as $loc) {
$locality[] = $loc;
}
foreach ($list_items as $lit) {
$listings[] = $lit;
}
foreach ($price as $p) {
$pricing[] = $p;
}
$i = -1;
foreach ($imgs as $sd) {
$i++;
$image_name = basename($sd);
$advert_posts = array(
'post_author' => $user_id_get,
'post_type' => 'advert',
'post_title' => $title[$i],
'post_content' => $description[$i] . $listings[$i],
'post_status' => 'publish',
);
$post_id = wp_insert_post($advert_posts);
$category_id = 877;
$taxonomy = 'advert-category';
wp_set_object_terms( $post_id, array( $category_id ), $taxonomy , true );
add_post_meta( $post_id , 'advert_sale_price' , $pricing[$i]);
add_post_meta( $post_id , 'advert_type' , 1);
add_post_meta( $post_id , 'advert_negotiable' , 1);
add_post_meta( $post_id , 'advert_location' , $locality[$i]);
var_dump( get_post_field( 'post_author', $post_id ), $user_id_get);
$upload = wp_upload_bits($image_name, null, file_get_contents($sd, FILE_USE_INCLUDE_PATH));
$imageFile = $upload['file'];
$wpFileType = wp_check_filetype($imageFile, null);
// Imposta meta per la galleria
$attachment = array(
'post_mime_type' => $wpFileType['type'], // tipo di file
'post_title' => sanitize_file_name($imageFile), // sanitizza e usa il nome dell'immagine come nome file
'post_content' => '', // potresti usare la descrizione dell'immagine come contenuto
'post_status' => 'inherit'
);
$attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
// genera e restituisce i metadati dell'allegato
$attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
// aggiorna e restituisce i metadati dell'allegato
wp_update_attachment_metadata($attachmentId, $attachmentData);
// infine, associa l'ID dell'allegato all'ID del post
$success = set_post_thumbnail($post_id, $attachmentId);
add_post_meta( $post_id , 'advert_gallery' , set_post_thumbnail($post_id, $attachmentId));
if ($success) {
echo 'Dati e immagini sono stati inseriti';
} else {
echo 'Qualcosa è andato storto';
}
}
wp_die();
}
}

ubaid ismail
11
Commenti
Mostra i restanti 7 commenti
Domande correlate
1
risposte
2
risposte
1
risposte