whenever someone purchases the product test, a new post is created and saved under the category with the post meta being set with key "order_id" being set to the order_id of the order.
add_action( 'woocommerce_after_checkout_validation', 'test3' );
function test3(){
global $woocommerce;
print_r($woocommerce);
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test post',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
I think you want to create a new post with postmeta 'order_id' => 45 after checkout completion? if its so, my code will help you.
add_action('woocommerce_new_order', 'order_check', 10, 1);
function order_check($order_id) {
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test post',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8, 39)
);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
add_post_meta($post_id, 'order_id', $order_id);
}
Related
Can't seem to find a definitive answer anywhere. I need to get the ID of a post after it's created with wp_insert_post().
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
wp_insert_post( $log_item );
After that, how do I get the ID of the just created $log_item post?
Please store the post id into temporary variable :
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
// You can also get the new post ID after inserting a new post:
$post_id = wp_insert_post( $log_item , $wp_error );
For more help : Click Here
I am trying to add a button in wordpress that deletes a custom-post-type post with a specific title and current user as author.
Problem that occurring is that all the posts gets deleted, all the job_alert posts, not only for this specific author or with this title.
Can someone see why?
$delete_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
I also have this code that creates a post and that works great. Similar code.
$new_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$post_id = wp_insert_post( $new_post );
For deleting the particular post, here is the solution,
Please use name parameter instead of post_title in your query, then only it will return the required post which you want.
I have modified your code. Please find the updated code below:
$delete_post = array(
'post_type' => 'job_alert',
'name' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
Now, your required post will be returned.
Hope, this may be helpful to you and let me know, if you have any query.
Thanks.
I'm trying to import woocommerce products with wp_insert_post() function but i have problem with 'post_category'. My code is
$my_post = array(
'post_content' => $description,
'post_name' => $product_name,
'post_title' => $product_name,
'post_status' => 'publish',
'post_type' => 'product',
'post_author' => 1,
'post_category' => array(9,10)// ids from woocommerce categories
);
wp_insert_post( $my_post );
But categories are empty. I tried 'product_cat' => array(9,10) but nothing again. Can anyone help me?
Try using wp_set_object_terms
$post_id = wp_insert_post( $my_post );
wp_set_object_terms($post_id, 9, 'product_cat', true);
wp_set_object_terms($post_id, 10, 'product_cat', true);
Also ensure that terms with id 9 & 10 already exist in product_cat taxonomy before importing the product.
You should use slug instead id:
wp_set_object_terms($post_id, get_term(9)->slug, 'product_cat', true);
wp_set_object_terms($post_id, get_term(16)->slug, 'product_cat', true);
If you put here new category slug, WordPress will create it, and attached product to this category.
I have the following code in a PHP function that is activated when I install my plugin that lets you create a post or page.
Works perfect and make the page if the $post_type is "post", but if the $post_type is "page", then it does not work, does not create the page.:
$my_post = array(
'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0
);
wp_insert_post( $my_post );
What is the problem? I can not find the solution.
Thank you very much!
I think you have to set the guid too, like this
$PageGuid = site_url() . "/my-page-req1";
$my_post = array( 'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0,
'guid' => $PageGuid );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.
I have a created a wordpress plugin that needs to create a page dynamically.
When the plugin is activated a page is created COOKIE_POLICY.
From my reading I found that inserting into the DB is the best way. And below is the way to do it. However, when I activate the plugin, there is no page or post created.
I go this from:
http://codex.wordpress.org/Function_Reference/wp_insert_post
function create_policy() {
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['COOKIE_POLICY'] ),
'post_content' => $_POST['Here is all our cookie policy info'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,30 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
This code is nearly correct. Below is the fixed code
function create_policy() {
$my_post = array(
'post_title' => 'cookiepolicy',
'post_content' => 'this is my content',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );