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
Related
I am trying to create or assign tags to a post by extracting keywords from a website with get_meta_tags() but I can't do it successfully.
for example, youtube has these meta keywords: video, sharing, camera phone, video phone, free, upload
$postarr = array(
'ID' => $post_id,
'post_author' => $post_author_id,
'post_content' => $post_content,
'post_title' => $post_title,
'post_excerpt' => $post_excerpt,
'post_status' => $post_status,
'post_type' => $post_type
);
$insert_update_post_id = wp_insert_post( $postarr );
$sitio =$form_data['_custom_field|link'];
$tags = get_meta_tags('https://www.youtube.com/');
wp_set_post_terms( $insert_update_post_id, $post_assign_terms, $tags['keywords'] );
Thank you for your help.
I registered a custom taxonomy with this code:
register_taxonomy( 'user_r_category', array( 'user_r' ), $args );
Now I try to insert a post to 'user_r_category' taxonomy within the category ID 7 and the post_type 'user_r':
$new_post = array(
//'ID' => '',
'post_author' => $current_user->ID,
//'post_category' => array(7),
'post_type' => 'user_r',
'post_content' => $r_textarea,
'post_title' => $r_title,
'tax_input' => array(
'user_r_category' => array( 7 )
),
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
The post was created, but not with the category 7. How could this work?
You would need to use:
1- get_term_by to get term obj
2- wp_set_object_terms to set custom taxonomy's term with post
$post_id = wp_insert_post($new_post);
$taxonomy = 'user_r_category';
$termObj = get_term_by( 'id', 7, $taxonomy);
wp_set_object_terms($post_id, $termObj, $taxonomy);
You can do it directly when inserting your post, with tax_input parameter :
$post = array(
'post_title' => $my_sanitized_title,
'post_content' => $my_sanitized_text,
'post_status' => 'publish',
'post_type' => 'post',
'tax_input' => array(
'hierarchical_tax' => array($my_hierarchical_tax_id)
),
);
$post_id = wp_insert_post($post);
This is an array. See the doc for detailed parameters : https://developer.wordpress.org/reference/functions/wp_insert_post/
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.
Im trying to insert the category
$apples = "granny smith"
as part of a new post.
I can create a new post with a TAG easily with the following script:
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'tags_input' => $apples,
'post_type' => 'post'
);
wp_insert_post( $my_post );
But for some reason, despite that the WP codex lists
'post_category'
for categories, the following code does NOT create the new category "granny smith" and instead, enters the new post as "uncategorized" :
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => $apples
'post_type' => 'post'
);
wp_insert_post( $my_post );
Can someone help me w the code please? Where am I going wrong?
Accordning to the documentation it looks like the categories should be added as an array with the category id's:
$id_for_category_ganny_smith = 12;
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($id_for_category_ganny_smith),
'post_type' => 'post'
);
wp_insert_post( $my_post );
The documentation states:
Categories need to be passed as an array of integers that match the
category IDs in the database. This is the case even where only one
category is assigned to the post.
If you don't know the id of the categories you want to add, you can try the following:
$apples_slug = "granny_smith"
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
);
$post_ID = wp_insert_post( $my_post );
wp_set_post_terms( $post_ID, $apples_slug, 'category')
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);