my question is about using wp_insert_post to insert post with type "photo"
not needs to upload the image right now just I want to insert the post information into the database
I used this code and it is working
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'photo',
'post_category' => array(3)
);
the question is I want to add the following informations
1- the photo type
2- I want to set the post as a featured Image
You first need to upload the file, then you can attach the image as an attachment to your post. This is the code I use to automate a blog:
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $postTitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, $postId);
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
Related
In Wordpress i have a function that correctly save a new processed image as attachment.
What i need is to set as featured image the attachment after its saving.
Here the function:
/* Save attachment */
$siteurl = get_option( 'siteurl' );
$file_info = getimagesize( $newFileName );
//create an array of attachment data to insert into wp_posts table
$tableParams = array(
'post_author' => get_current_user_id(),
'post_date' => current_time( 'mysql' ),
'post_date_gmt' => current_time( 'mysql' ),
'post_title' => $_POST['title'] . '-' . $_POST['filter'],
'post_status' => 'inherit',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_name' => sanitize_title_with_dashes( str_replace( "_", "-", $_POST['name'] . '-' . $_POST['filter'] ) ),
'post_modified' => current_time( 'mysql' ),
'post_modified_gmt' => current_time( 'mysql' ),
'post_parent' => 0,
'post_type' => 'attachment',
'guid' => $newFileName,
'post_mime_type' => $file_info['mime'],
'post_excerpt' => '',
'post_content' => ''
);
// insert the database record
$attachmentID = wp_insert_attachment( $tableParams, $newFileName, 0 );
// generate metadata and thumbnails
if ( $attachmentData = wp_generate_attachment_metadata( $attachmentID, $newFileName ) ) {
wp_update_attachment_metadata( $attachmentID, $attachmentData );
}
And i call it in javascript:
ajaxcall = jQuery.post(ajaxparam.ajax_url,
{
action: 'new_image',
image: imageData,
format: format,
title: imageTitle,
name: imageName,
filename: largeImage,
filter: effect,
nonce: ajaxparam.nonce,
});
jQuery.when(ajaxcall).done(function(){
editor.insertContent(..............................);
});
So i tried to add to the function:
[......]
if ( $attachmentData = wp_generate_attachment_metadata( $attachmentID, $newFileName ) ) {
wp_update_attachment_metadata( $attachmentID, $attachmentData );
}
global $post;
set_post_thumbnail($post->ID, $attachmentID);
or
add_post_meta($post->ID, '_thumbnail_id', $attachmentID);
or
update_post_meta( $post->ID, '_thumbnail_id', $attachmentID );
in any case without success.
Thanks for any suggestion.
I want to connect post with an image, but it doesn't work.
The code that I have tried to use:
$my_post = array(
'post_title' => $movies->names->cs,
'post_content' => "<p>".$movies->plot."</p>",
'post_status' => 'publish',
'post_author' => 1,
);
$postik = wp_insert_post( $my_post );#<-- there i add post
$img = array(
'post_title' => $postik,
'post_content' => "",
'post_status' => 'inherit',
'post_parent' => "$postik",
'guid' => $movies->poster_url,
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'post_author' => 1,
);
wp_insert_post( $img ); # <-- there i add image with URL
set_post_thumbnail( $postik, $img );#<-- there i connect post with thumb
But on post is image not showing.
In your code $img is the array with arguments you use in wp_insert_post( $img );.
You're currently using this as the second argument of set_post_thumbnail() which is not right.
Instead use:
$thumbnail_id = wp_insert_post( $img ); // $thumbnail_id now holds the ID of the inserted image
set_post_thumbnail( $postik, $thumbnail_id ); // Add the thumbnail (ID) to the post
I'm trying to insert an image into a custom post type. I've read to use wp_generate_attachment_metadata after using media_handle_upload.
$pid = wp_insert_post($new_post);
$attachment_id = media_handle_upload( 'art_upload[$i]', $pid );
$attach_data = wp_generate_attachment_metadata( $attachment_id, $file);
wp_update_attachment_metadata( $attachment_id, $attach_data );
How do i get the variable $file?
i used this code to insert image in database
<?php
$post = array(
'post_content' => $method, // The full text of the post.
'post_name' => $recipetitle, // The name (slug) for your post
'post_title' => $recipetitle, // The title of your post.
'post_status' => 'publish', // Default 'draft'.
'post_type' => 'recipespost', // Default 'post'.
'post_author' => 1, // The user ID number of the author. Default is the current user ID.
'menu_order' => '0', // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
'post_password' => '', // Password for post, if any. Default empty string.
'post_excerpt' => '', // For all your post excerpt needs.
'comment_status' => 'open' , // Default is the option 'default_comment_status', or 'closed'.
'post_category' => $recipetype, // Default empty.
);
$post_id = wp_insert_post( $post);
wp_set_post_categories( $post_id);
add_post_meta($post_id, 'diet', $dyettype, true);
$filename = $_FILES["recipeimage"]["name"];
$uploads = wp_upload_dir();
//file_put_contents($file, $image_data);
$file_array = array(
'name' => $_FILES['recipeimage']['name'],
'type' => $_FILES['recipeimage']['type'],
'tmp_name' => $_FILES['recipeimage']['tmp_name'],
'error' => $_FILES['recipeimage']['error'],
'size' => $_FILES['recipeimage']['size'],
);
// required for wp_handle_upload() to upload the file
$upload_overrides = array( 'test_form' => FALSE );
if ( !empty( $file_array['name'] ) ) {
// upload the file to the server
$uploaded_file = wp_handle_upload( $file_array, $upload_overrides );
// checks the file type and stores in in a variable
$wp_filetype = wp_check_filetype( basename( $uploaded_file['recipeimage'] ), null );
// set up the array of arguments for "wp_insert_post();"
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename( $uploaded_file['recipeimage'] ) ),
'post_content' => '',
'post_author' => $logged_in_user,
'post_status' => 'inherit',
'post_type' => 'attachment',
'guid' => $uploads['url'] . '/' . $file_array['name']
);
// insert the attachment post type and get the ID
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file['recipeimage'] );
require_once(ABSPATH . 'wp-admin/includes/image.php');
// update the attachment metadata
wp_update_attachment_metadata( $attach_id, $attach_data );
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
set_post_thumbnail( $post_id, $post_thumbnail_id );
}
?>
The following code add post in db but not uploading files and not creating thumbnails,
$filename=$_FILES["image1"]["tmp_name"];
$post_id=1179;
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_name' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_parent' => $post_id,
'post_excerpt' => $thumb_credit,
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
if($attachment_id != 0) {
wp_update_attachment_metadata($attachment_id, $attach_data);
update_post_meta($post_id, '_thumbnail_id', $attachment_id);
}
its working to post in db but image is not uploading,
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
As per documentation you should have reference to the image.php file.
I'm struggling a bit to insert a post with the features I require.
include ('../wp-load.php');
$my_post = array(
'post_title' => 'title' ,
'post_content' => 'some content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(34,35),
'tags_input' => array('tag1,tag2'),
'the_post_thumbnail' => 526
);
// Insert the post into the database
wp_insert_post( $my_post );
Question 1:
It's all working besides for 'the_post_thumbnail' => 526 - I was hoping that was going to attach the media item id (526) as the featured post image (obviously this isn't working). What is the correct way to do this?
Question 2:
Is there a way to get the URL of the post that is created?
Please try the following example that uses the functions set_post_thumbnail() and get_permalink():
$my_post = array(
'post_title' => 'title' ,
'post_content' => 'some content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(34,35),
'tags_input' => array('tag1,tag2'),
);
// Insert the post into the database
$pid = wp_insert_post( $my_post );
if( is_wp_error( $pid ) )
{
// Display error:
echo $pid->get_error_message();
}
else
{
// Set featured image to inserted post:
set_post_thumbnail( $pid, 526 );
// Get permalink:
$link = get_permalink( $pid );
}
where we use is_wp_error() to make sure the insert was sucessful.
Hope this helps.