The wordpress function is used for submitting data programatically. Standard fields to submit to incude the content, excerpt, title, date and many more.
What there is no documentation for is how to submit to a custom field. I know it is possible with the add_post_meta($post_id, $meta_key, $meta_value, $unique); function.
What I don't know is how to include that into the standard wp_insert_post function. So the reason I ask you all is because this is more of a PHP question than a WP question. Below is the PHP code to submit the post.
<?php
$my_post = array(
'post_title' => $_SESSION['booking-form-title'],
'post_date' => $_SESSION['cal_startdate'],
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'booking',
);
wp_insert_post( $my_post );
?>
Any help chaps,
Marvellous
If you look at the functions reference in the codex, you can see that wp_insert_post returns The ID of the post if the post is successfully added to the database.
Because of that, you can do so:
<?php
$my_post = array(
'post_title' => ...
);
$new_post_id = wp_insert_post( $my_post );
add_post_meta($new_post_id, $meta_key, $meta_value, $unique);
?>
Hope this helps
Related
I want to create system to insert wordpress post from front-end to logged user. But I have checked same title post many times added by different user.
How I add error message if post title already exist
Here is my code
$u_id = get_current_user_id();
$my_post = array(
'post_title' => 'title',
'post_content' => 'this is post content',
'post_status' => 'publish',
'post_author' => $u_id,
'post_type' => 'customer_post',
);
$post_ID = wp_insert_post( $my_post );
You can prevent this by checking some condition. You should try this. Wrap your code within this condition, it will not allow post with same title.
if (!get_page_by_title($title, 'OBJECT', 'post') ){
//your code goes here.
}
I'm new to Wordpress and php,so sorry for my basic question.
I want to add some posts in a category programmatically.
For example every day i want to select some posts and insert them into a category with php code and for next day replace some other posts.
Is it possible? How?
Thanks in advance.
You can use wp_insert_post https://codex.wordpress.org/Function_Reference/wp_insert_post
See the Codex, here you have a lot of examples:
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
To update the Post see wp_update_post https://codex.wordpress.org/Function_Reference/wp_update_post
Use wp_set_post_categories
wp_set_post_categories( $post_id, array( 1, 2 ) );
i'm doing an webapp that works with a wordpress website.
Until yesterday all works like a charm, but from today, when i save the post on wordpress db, it now not working...
Not give me error (php error or wordpress error), but simply a white page.
This is my full code:(sorry for many commented lines and echo, but i'm testing)
The important part:
// Create post object
$my_post = array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post($my_post, TRUE);// try first time
$post_status = "pending";
$post_type = "post";
$post_parent = "0";
$post_ID = (int) $wpdb->insert_id;
echo $post_ID; die;
Yesterday with a simple wp_insert_post(my_post); all worked fine, today (of course i think i edited some files...or i don't now), not works more and not give me error.
What do you think ?
Thank you, really!
EDIT: The problem was that i not insert post_category and wp need it!
You can capture the $post_id with the wp_insert_post() function...
$post_ID = wp_insert_post($my_post, TRUE);// try first time
Then you can dump out the $post_ID variable to get the ID or the error.
var_dump($post_ID);
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title($article_title,'OBJECT','post') == NULL )
$my_post= array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_type' => 'post',
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
echo 'Post ID - '.$post_id;
}
I have been trying this project of mine for a while now and I have got no luck. I'm trying to create 8 post programmatically in functions.php. I need these to only post 1 time. The problem I've been having is everytime I refresh the page the post will just auto create more. Here is my code to create post progammatically in functions.php.
<?php // Create post object
$my_post = array(
'post_title' => 'How to make your diet success',
'post_name' => '7-ways-to-make-succes-Diet',
'post_content' => 'my content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post ); ?>
The only proble with this code is that it auto creates more post every time the page refreshes. I'm going to be creating 8 of these functions and I only want them to be created once. A code example would be great.
Next, I want to display the post on my index.php. I want to get these post individually. Here is my code I have so far.
<div class="post1"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
<div class="post2"><?php $post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta ?> </div>
I'm pretty sure I need to either call the slugs or post name to get them individually. Yes, I have tried this method as well as 10 other methods but nothing has worked. The closest I got was it to display the post name. Code examples would be great. I will be so grateful and probably donate some money via paypal if someone can get this working for me. Thanks.
functions.php is not a good place for programatically create pages nor posts. You should create a plugin (it is simple as creating custom theme) and create posts in its activation function. This function is called only on your plugin activation. Read also about plugin deactivation a uninstall hooks
The reason why your posts get created again and again is that file functions.php is called each time the page is requested. If you insist on creating posts in functions.php, you should wrap your wp_insert_post by a condition chcecking whether your posts are already created - than get_posts function would suit your needs.
<?php
//Use either post slug (post_name)
$post = get_posts( array( 'name' => '7-ways-to-make-success-diet' ) );
/*or $post = get_posts( array( 'name' => sanitize_title('My Single.php Test') ) );
if you do not set the post_name attribute and let WordPress to set it up for you */
if ( empty($post) ) {
// Create post object
$my_post = array( 'post_title' => 'My Single.php Test', 'post_name' => '7-ways-to-make-success-diet', 'post_content' => 'my content4654654', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) );
// Insert the post into the database
wp_insert_post( $my_post );
}
?>
Also, get_posts will help you to bring your posts on front page. Eg.
<?php
$post = get_posts( array( 'name' => 'How to make your diet success' ) );
echo $post->post_title;
...
?>
I am trying to insert a post with this code:
$my_post = array(
'post_type' => "essays",
'post_title' => 'TEST 3',
//'post_content' => $content,
'post_status' => 'draft',
'post_author' => 1,
//'post_category' => $cat,
'tags_input' => 'TQM,tag',
);
$post_id = wp_insert_post($my_post);
Everythings works ok except the tags, it does not insert any of them.
Any idea?
Use the wp_set_object_terms() function:
http://codex.wordpress.org/Function_Reference/wp_set_object_terms
wp_set_object_terms($post_id , $arrayoftags, $name_of_tag_taxonomy, false);
Good luck
Your post type is essays. Custom post types do not support tags by default. You'll have to add a tags taxonomy to them.
http://codex.wordpress.org/Taxonomies
http://codex.wordpress.org/Function_Reference/register_taxonomy
To insert the post with tags and categories do this
$pid=wp_insert_post($new_post);
wp_set_post_terms( $pid, $arrayoftags);
wp_set_post_categories( $pid, $arrayofcategories );
so $pid is the post id basically you first insert the post without tags or categories and the function returns the post's id which you can then use to insert the tags and categories each with their respective function, if you look at the source code of wp_insert_post you will notice that the function works in a different way for custom post types, I did not look more into it as I don't want to hack the code as there is a better solution by using the built in functions
Hi I found this answer somewhere and this might help you out
//first get the term (I used slug, but you can aslo use 'name'), see: http://codex.wordpress.org/Function_Reference/get_term_by
$term = get_term_by( 'slug', 'your custom term slug', 'your custom taxonomy' );
//then get the term_id
$term_id = $term->term_id;
//Use 'tax_input' instead of 'post_category' and provide the term_id:
'tax_input' => array( 'your taxonomy' => $term_id )
Hope that helps.
tags and post categories ought to be entered as an array, even if it's only one. So 'tags_input' => 'TQM,tag' should be 'tags_input' => array('TQM,tag')