Post inserat programatic în WordPress nu apare în profilul autorului
18 iun. 2022, 14:17:53
Vizualizări: 74
Voturi: 1
Am inserat un post programatic în WordPress. Când vizualizez postarea pe pagina autorului, postarea inserată programatic nu apare, dar după ce actualizez manual postul, funcționează și se afișează corect. Care ar fi cauza și cum pot rezolva această problemă? Există vreun ghid?
Codul din plugin-ul meu:
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);
// setare meta pentru galerie
$attachment = array(
'post_mime_type' => $wpFileType['type'], // tipul fișierului
'post_title' => sanitize_file_name($imageFile), // sanitizare și utilizare nume imagine ca nume fișier
'post_content' => '', // poți folosi descrierea imaginii aici ca conținut
'post_status' => 'inherit'
);
$attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
// generare și returnare metadate atașament
$attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
// actualizare și returnare metadate atașament
wp_update_attachment_metadata($attachmentId, $attachmentData);
// asociere ID atașament la ID post
$success = set_post_thumbnail($post_id, $attachmentId);
add_post_meta( $post_id , 'advert_gallery' , set_post_thumbnail($post_id, $attachmentId));
if ($success) {
echo 'Datele și imaginile au fost inserate';
} else {
echo 'Ceva nu a funcționat corect';
}
}
wp_die();
}
}

ubaid ismail
11
Comentarii
Arată celelalte 7 comentarii
Întrebări similare
1
răspunsuri
2
răspunsuri
1
răspunsuri