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.
Related
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'post_status' => 'private',
'post_type' => 'student'
);
wp_insert_post($student);
This is how I'm inserting post from the frontend. it is working fine.
But I need to save some custom fields also from frontend along with post and need show the values in backend(just in text format not in input field). So is there anything exists like this 'my_custom_filed' => 'custom_value':
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'my_custom_filed' => 'custom_value'
'post_status' => 'private',
'post_type' => 'student'
);
wp_insert_post($student);
So I can print the value of custom fields in backend
function studentDataMetaBoxStructure(){
global $post;
$my_custom_filed = get_post_meta($post->ID,'my_custom_filed',true);
echo $my_custom_filed;
}
But I'm getting nothing in $my_custom_filed;
This is how you save meta box while post insertion.
$student = array(
'post_title' => wp_strip_all_tags( $student_name ),
'my_custom_filed' => 'custom_value'
'post_status' => 'private',
'post_type' => 'student'
);
$the_post_id = wp_insert_post($student);
update_post_meta( $the_post_id, 'custom_meta_box_key', 'custom_meta_box_value' );
I have created a frontend form that crates a new custom post type single. Is there anyway to update the ACF meta from this form?
<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Add the content of the form to $post as an array
$post = array(
'post_title' => $description,
'post_content' => $description,
'post_category' => 3, // Usable for custom taxonomies too
'post_status' => 'private', // Choose: publish, preview, future, etc.
'post_type' => 'door', // Use a custom post type if you want to
'post_parent' => $post->ID,
'post_name' => 'service',
'meta_key' => 'archive_job',
'meta_value' => 'red',
);
wp_set_object_terms($pid, $_POST['terms'], 'child');
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
wp_redirect( $current_url );
// http://codex.wordpress.org/Function_Reference/wp_insert_pos
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post', 'add_post_meta' , 'update_post_meta');
?>
ACF meta are saved in database as basic custom field. So you can use for example function add_post_meta() with values add_post_meta($post_id, 'field_name', $field_value); after wp_insert_post($post); function.
example code:
$post = array(
'post_title' => $description,
'post_content' => $description,
'post_category' => 3, // Usable for custom taxonomies too
'post_status' => 'private', // Choose: publish, preview, future, etc.
'post_type' => 'door', // Use a custom post type if you want to
'post_parent' => $post->ID,
'post_name' => 'service',
'meta_key' => 'archive_job',
'meta_value' => 'red',
);
wp_set_object_terms($pid, $_POST['terms'], 'child');
$post_id = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
add_post_meta($post_id, 'field_name', $field_value);
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
wp_redirect( $current_url );
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 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
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')