I am currently working on a word press custom post, where I fetch content from mysql database and programatically create the post using wp_insert_post.After doing this title and description is fetched and displayed as expected. However the image is not being displayed instead url alone is getting printed.
Please find my code related to the image display (I am not sure any other tags need to added).
'post_content' => wp_strip_all_tags($result->descriptions.''.$result->image ),
please find the full code related to fetching and display logic.
function techiepress_query_garage_table( $car_available_in_cpt_array ) {
global $wpdb;
$table_name = $wpdb->prefix . 'garage';
if ( NULL === $car_available_in_cpt_array || 0 === $car_available_in_cpt_array || '0' === $car_available_in_cpt_array || empty( $car_available_in_cpt_array ) ) {
$results = $wpdb->get_results("SELECT * FROM $table_name");
return $results;
} else {
$ids = implode( ",", $car_available_in_cpt_array );
$sql = "SELECT * FROM $table_name WHERE id NOT IN ( $ids )";
$results = $wpdb->get_results( $sql );
return $results;
}
}
function techiepress_insert_into_auto_cpt() {
$car_available_in_cpt_array = techiepress_check_for_similar_meta_ids();
$database_results = techiepress_query_garage_table( $car_available_in_cpt_array );
if ( NULL === $database_results || 0 === $database_results || '0' === $database_results || empty( $database_results ) ) {
return;
}
foreach ( $database_results as $result ) {
$car_model = array(
'post_title' => wp_strip_all_tags( $result->Brand),
'post_content' => wp_strip_all_tags($result->descriptions.''.$result->image ),
'meta_input' => array(
'id' => $result->id,
'brand' => $result->Brand,
'model' => $result->Model,
'color' => $result->Color,
'km' => $result->Km,
),
'post_type' => 'auto',
'post_status' => 'publish',
);
wp_insert_post( $car_model );
}
}
It's not super clean, but this kind of trick does the job ?
'post_content' => wp_strip_all_tags($result->descriptions.' <img src="'.$result->image.'" />' ),
Otherwise by following WordPress logic
....
$postId = wp_insert_post( $car_model );
if(!empty($result->image)){
$upload = wp_upload_bits('My car picture' , null, file_get_contents($result->image, FILE_USE_INCLUDE_PATH));
// check and return file type
$imageFile = $upload['file'];
$wpFileType = wp_check_filetype($imageFile, null);
// Attachment attributes for file
$attachment = array(
'post_mime_type' => $wpFileType['type'], // file type
'post_title' => sanitize_file_name($imageFile), // sanitize and use image name as file name
'post_content' => '', // could use the image description here as the content
'post_status' => 'inherit'
);
// insert and return attachment id
$attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );
// insert and return attachment metadata
$attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile);
// update and return attachment metadata
wp_update_attachment_metadata( $attachmentId, $attachmentData );
set_post_thumbnail( $postId, $attachmentId );
}