I created a function, which duplicate products for specific users, when another user submit a post form.
In general I don't have issues to duplicate the products. When user ID 151 submit the post form, the product is duplicated for the other two users in the array under variable $relatedVendors. However, I have one issue within the function. As you can see I take the wpuf form ID from the original product and would like to store this id also for the other duplicated products (posts). For this case I use get_post_meta for the original post and then add_post_meta for the duplicated posts in the foreach loop. Now my issue is, that the meta key is populated for the duplicated posts, but the wpuf ID is not in the value field.
I tried to figure out, what the issue is. So the add_post_meta seems to work, because in one run I have hard coded an integer instead of $wpuf variable. In this case the hard coded id was stored in the meta value field. The same works fine for the second add post value "catalog".
For me it looks like that the get_post_meta is not working. I moved this part right below get the user id, then within the if state and within the foreach loop. I changed the priority as well. So, know I'm asking for help, why the get post is not working.
add_action('save_post', 'myWoo_duplicatePost', 10, 2);
function myWoo_duplicatePost($postID, $post) {
$current_user = wp_get_current_user();
if (isset($post->post_type) && $post->post_type == 'product' && $post->post_modified_gmt == $post->post_date_gmt && $current_user->ID == 151 ) {
$relatedVendors = array(114,88);
if ($post != null) {
remove_action('save_post', 'myWoo_duplicatePost', 10, 2);
foreach ($relatedVendors as $vendor) {
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $vendor,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'publish',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$new_post_id = wp_insert_post( $args );
$wpufID = get_post_meta($post_id, '_wpuf_form_id', true );
add_post_meta( $new_post_id, '_wpuf_form_id', $wpufID );
add_post_meta( $new_post_id, '_visibility', 'catalog' );
}
}
add_action('save_post', 'myWoo_duplicatePost', 10, 2);
}
}
Related
I wonder if there is any way to have only one wordpress instant which has 2 different domains (one main domain and one subdomain). The content of the domains should be different but also share pages and templates and posts. E.g. a page "employees" should be the same for both domains.
I have already set up a WordPress network and so the multi-domain setup works but unfortunately pages and posts and templates are separated. There are plugins which mirror e.g. posts also on the other domain but here is then again the problem that I have saved the post 2 times.
Maybe you already have a plugin which can do this, without any coding, but i'm not familiar with them.
An idea would be to hook onto the save_post action and push the newly created post to the different blogs.
if ( ! function_exists( 'post_to_multiple_sites' ) ) {
add_action( 'save_post', 'post_to_multiple_sites', 20, 2 );
function post_to_multiple_sites($original_id, $original_post) {
// To prevent publishing revisions
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $original_id;
}
// We only want to mess around with a post that has been published
if( 'publish' !== get_post_status( $original_post ) ) {
return $original_id;
}
// prevent "Fatal error: Maximum function nesting level reached"
remove_action( 'save_post', __FUNCTION__ );
/**
* If i'm correct, when creating a network, each site/blog receive an ID.
* You can set these hardcoded, or create a function that returns an array of the blog id's.
*
* If you only have a couple of sites, I would maintain this manually. Otherwise create a function to
* gather all the ID's and return it as an array (up to you).
*/
$blog_ids = [2, 3];
$post_data = [
'post_author' => $original_post->post_author,
'post_date' => $original_post->post_date,
'post_modified' => $original_post->post_modified,
'post_content' => $original_post->post_content,
'post_title' => $original_post->post_title,
'post_excerpt' => $original_post->post_excerpt,
'post_status' => 'publish', // new post will be set to published
'post_name' => $original_post->post_name,
'post_type' => $original_post->post_type,
];
// Gather the post meta & terms
$post_terms = wp_get_object_terms( $original_id, 'category', array( 'fields' => 'slugs' ) );
$post_meta = get_post_custom( $original_id );
foreach ($blog_ids as $blog_id) {
switch_to_blog($blog_id); // https://developer.wordpress.org/reference/functions/switch_to_blog/
// IN case a post with the same slug exists, don't do anything.
// Or maybe create a new post with slug-name-2...up to you.
if ( get_posts( [ 'name' => $post_data[ 'post_name' ], 'post_type' => $post_data[ 'post_type' ], 'post_status' => 'publish' ] ) ) {
restore_current_blog();
continue;
}
$inserted_post_id = wp_insert_post( $post_data );
wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false );
foreach ( $post_meta as $meta_key => $meta_values) {
// we do not need these redirects
if( '_wp_old_slug' === $meta_key ) {
continue;
}
foreach ( $meta_values as $meta_value ) {
add_post_meta( $inserted_post_id, $meta_key, $meta_value );
}
}
restore_current_blog();
}
}
}
It became a bigger function than I expected and honestly don't know if you should go this route. Your safest bet is to try and find a plugin that handles this for you. But you can use this piece of code and extend it obviously to your needs
Make sure to read the comments I added. These are important!
I am currently creating a plugin that collects data from their license plate. Everything works just fine. The post gets created, I receive the ID of the created post and etc. But at the very end of my function.. I'd like to update post meta doing so:
update_post_meta($product_id, '_regular_price', '0');
update_post_meta($product_id, '_subscription_period_interval', '1');
update_post_meta($product_id, '_subscription_period', 'week');
But nothing happens. I've tried running the function again placing the code above somewhere else and that I've tried multiple places inside my code. Even tried creating a whole new function for those 3 lines itself. Still no luck.
I tried returning the post meta from the post that I just created. Looking inside the returned data I see my post_meta was set, but when visiting the post from the WordPress post type archive, the post meta was reset/ignored.
Here's what I'm facing:
As you can see, the post meta is being set but when looking inside my archive... Not set.
I even tried with sleep(3) to make sure the post was created before update_post_meta. No luck.
Any ideas or tips for how I can resolve this?
I'd like to add that by taking these 3 lines and adding them to example: functions.php, and then hardcoding id's then it will work. So the short version is that when creating the post, the 3 lines do nothing even if the id is set correctly. So it has to be something to when creating the post etc...
Edit
You asked to see where the product_id is being set:
$post_data = array(
'post_author' => $author,
'post_name' => $postname,
'post_title' => $data['title'],
'post_content' => $data['content'],
'post_excerpt' => $data['excerpt'],
'post_status' => 'draft',
'ping_status' => 'closed',
'post_type' => 'product',
);
// Creating the product (post data)
$product_id = wp_insert_post( $post_data );
WordPress #WooCommerce
First it seems that you are trying to create a simple subscription product… So you forgot to set the product type which is subscription and also some other things related to product subscription price.:
$post_data = array(
'post_author' => $author,
'post_name' => $postname,
'post_title' => $data['title'],
'post_content' => $data['content'],
'post_excerpt' => $data['excerpt'],
'post_status' => 'draft',
'ping_status' => 'closed',
'post_type' => 'product',
);
// Creating the product (from post data)
$product_id = wp_insert_post( $post_data );
// Set the product type <==== <==== <==== Missing
wp_set_object_terms( $product_id, 'subscription', 'product_type' ); // <== Missing
$product_price = 0;
update_post_meta($product_id, '_regular_price', $product_price);
update_post_meta($product_id, '_price', $product_price); // <== <== <== Missing
update_post_meta($product_id, '_subscription_price', $product_price); // <== Missing
update_post_meta($product_id, '_subscription_period', 'week');
update_post_meta($product_id, '_subscription_period_interval', '1');
It should better work now.
I know this might seem strange to some, but i would like to duplicate a post on creation.
When a post is created, i would like to duplicate it appending new data to the title, as well as updating meta fields and changing the taxonomy it is in.
Here is what i have done so far:
add_action('wp_insert_post', 'my_add_custom_fields');
function my_add_custom_fields($post_id)
{
if ( $_POST['post_type'] == 'products' ) {
$my_post = array(
'post_title' => get_the_title(),
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'products',
);
$id = wp_insert_post($my_post);
update_post_meta($id,'keywords', get_the_title());
wp_set_object_terms($id, 'New Term Here', 'platform');
}
return true;
}
The issue i have is this creates an endless loop, creating the new post thousands of times and wont stop until i restart apache.
Is there away around this?
You need some sort of control for this to stop it looping. e.g. set a global value to count
$GLOBALS['control']=0;
add_action('wp_insert_post', 'my_add_custom_fields');
function my_add_custom_fields($post_id)
{
if ( $_POST['post_type'] == 'products' ) {
//if control is on third iteration dont proceed
if($GLOBALS['control']===2)
return;
//add control here!
$GLOBALS['control']++;
$my_post = array(
'post_title' => get_the_title(),
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'products',
);
$id = wp_insert_post($my_post);
update_post_meta($id,'keywords', get_the_title());
wp_set_object_terms($id, 'New Term Here', 'platform');
}
return true;
}
I'm using acf with reapeters.
I'm trying to add a row to a repeater through update_field, however, when I open a post to confirm if it works in wordpress' admin, it kept loading and gave me this error:
Fatal error: Maximum execution time of 30 seconds exceeded.
When I echo $value, I get this: "bool(false)", here's my code:
foreach($dishes as $current_dish)
{
$local_category_id = strval($current_dish['id_CategoriaTicket']);
if($local_category_id == $categoryId)
{
$prueba = $current_category->post_title;
$new_dish = array(
'post_title' => $current_dish['Descripcion'],
'post_status' => 'publish',
//'post_content' =>'',
'post_type' => 'products'
//'post_author' => $user->ID
);
$post_id = wp_insert_post( $new_dish );
global $cpt_onomy;
$cpt_onomy->wp_set_post_terms( $post_id , $prueba, 'categories');
update_post_meta($post_id, 'product_type', "Platillo");
$field_key = 'product1';
$value = get_field($field_key, $post_id);
$value[] = array("price_id" => "2",
"price" => "30",
"location" => 2
);
update_field($field_key, $value, $post_id );
exit();
}
}
the problem is that the field key you are using is not the correct one.
you need to go to custom fields menu and from the top screen options show the field key ids.
then copy the field key Id of the repeater and use it in the update_field method.
is you use field name you will get an infinite loop and a fatal error due to memory leakage.
In Wordpress, I have a function which allows me to automatically add a woocommerce product by adding a new post of a custom post type called 'daily_cartoon'.
This the code of the function:
function convert_daily_cartoon_into_product($post_id){
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
// Create post object
$post = array(
'post_title' => $_POST['post_title'],
'post_type' => 'product',
'post_status' => 'publish',
'tax_input' => array( 'product_cat' => 14 )
);
// Insert the post into the database
$new_print = wp_insert_post( $post );
update_post_meta( $new_print, '_thumbnail_id', get_post_thumbnail_id( $post_id ) );
update_post_meta( $new_print, '_regular_price', 5 );
update_post_meta( $new_print, '_price', 5 );
}
}
add_action('publish_daily_cartoon', 'convert_daily_cartoon_into_product');
The new product shows up nicely in the admin area, with all the data there - i.e. price, category, published, etc.
But for some reason it doesn't show up on the website shop page until I open the new product to edit and click update.
Anybody?
Make sure you set the _visibility meta of the product to visible
For those comming here
Don't forget to check your datas, I got a similar problem and it's because WP sanitize datas once you update them manually from the dashboard. That's why it worked for those updated manually.
Into my database, my datas got some hidden spaces (example : "Paris" / " Paris").
In case you need to import products, consider to use the wp function sanitize_text_field()