Okay guys, here's the scenario.
I'm am trying to setup a function that will automatically duplicate a post (when published) over to another post type. So, a regular blog post is published, and when it is published, all of its information is copied over to a custom post type (for WP ECommerce), automatically creating a store entry for that blog post.
I've written a function that takes care of picking up all of the information and inserting it into a new post with wp_insert_post(). It all works great, except for one thing. I'm using wp_set_object_terms() to set the store category ids of the new product based on the tags of the blog post. However, for some reason, wp_set_object_terms() never works.
Now, here's the catch. I'm running all of this on a multi site install and am using threeWP_Broadcast to allow me to cross publish posts. The blog posts (which have to be copied to the store) are published from a sub site and broadcasted to the main site. The store is on the main site.
So I've hooked my custom function into the broadcast plugin so that it gets fired when a post is broadcasted from a sub site to the main site.
Everything with my function works great, except for the wp_set_object_terms()
Here's function:
function copy_post_to_store_product() {
global $blog_id;
global $wpdb;
// only copy the post over if on main site, not subsites
if ($blog_id == 1) {
$args = array('numberposts' => 1);
$latest = get_posts($args);
foreach($latest as $post) : setup_postdata($post);
// if NOT Tip and NOT source avail, create product
// if source, price is ALWAYS $4
// auto create for source files only -- regular post type
$custom_meta = get_post_custom($post->ID);
// what kind of post is this?
$post_type = $custom_meta['cgc_post_type'][0];
// does this post have a source file product associated with it?
$source_avail = $custom_meta['cgc_source_avail'][0];
// source file price
$price = '4';
$product_info = array(
'post_title' => $post->post_title,
'post_type' => 'wpsc-product',
'post_content' => $post->post_content,
'post_author' => $post->post_author,
'post_status' => 'draft',
);
if($post_type == 'Regular' && $source_avail == true) {
// only auto create product for Regular posts that have source files for sale
$product_id = wp_insert_post($product_info);
update_post_meta($product_id, '_wpsc_price', $price);
if (has_tag('blender', $post->ID))
{
$product_cats = array(23,32);
}
elseif (has_tag('max', $post->ID)) {
$product_cats = array(23,34);
}
elseif (has_tag('modo', $post->ID)) {
$product_cats = array(23,19);
}
// set the product categories
wp_set_object_terms($product_id, $product_cats, 'wpsc_product_category' );
}
endforeach;
}
}
The function works by retrieving the latest post from the main site ($blog_id == 1) and copying all of its information into variables for wp_insert_post().
Now, the really interesting thing is that works perfectly if I attach the function to a publish_post hook, but, unfortunately, I can't do that because that hook doesn't fire when a post is broadcasted.
Any ideas would be hugely appreciated.
wp_set_object_terms()
return for you? – Rarst Apr 29 '11 at 9:40var_dump()
– Rarst Apr 29 '11 at 18:42