How to assign new array of custom taxonomy to newly inserted post in a cron job
-
Hi There,
I have a problem that is driving me literally nuts.
I have a cron job that runs and gets infomration via a CURL request for new products and updated products.I can insert a new post.
I can insert new terms, taxonomiesWhat I cannot seem to do with the post it, the taxonomy id or term id is associated them together.
I have tried three functions that are supposed to do this:
wp_set_object_terms( $ID, $post_categories, ‘product_cat’, true);
wp_set_post_categories( $ID, $post_categories, false);
wp_set_post_terms( $ID, $post_categories);None of these work at all as they do not enter information in the term_relationships table.
I also tried inserting the new terms into the post data when inserting or updating, the post categories which as I understand is now longer used.
The categories get created first if not existing and if exist return the ID into a new array.
Here is the complete code…
private function setEKMStock($object, $url, $apiKey, $clientID) { global $wpdb; $count = 0; $parser = new ArrayObject(); /*$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/"><soap:Body>'; $xml_post_string .= '<SetProductStock xmlns="https://publicapi.ekmpowershop.com/"><SetProductStockRequest><APIKey>' . $apiKey . '</APIKey>';*/ // Post thumbnail support if ( ! current_theme_supports( 'post-thumbnails', 'product' ) ) { add_theme_support( 'post-thumbnails' ); remove_post_type_support( 'post', 'thumbnail' ); remove_post_type_support( 'page', 'thumbnail' ); } else { add_post_type_support( 'product', 'thumbnail' ); } // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . 'wp-admin/includes/media.php' ); // Add image sizes $shop_thumbnail = wc_get_image_size( 'shop_thumbnail' ); $shop_catalog = wc_get_image_size( 'shop_catalog' ); $shop_single = wc_get_image_size( 'shop_single' ); ///var_dump($object[0]); foreach($object as $key => $value){ //// check if update or insert post $name = $value['Name']; $sku = $value['SKU']; $shortDescription = $value['ShortDescription']; $filename = $value['Photo']; $price = $value['Price']; $packQty = $value['PackQty']; $stock = $value['Stock']; $post_categories = array(); foreach($value['Category'] as $key => $value ){ //// check if exists, get category or if not exists then create the category $theCat = get_term_by( 'slug', sanitize_title_with_dashes(trim(strtolower($value))), 'product_cat'); if($theCat == NULL) { $term = wp_insert_term ( trim(strtolower($value)), 'product_cat', array( 'description'=>trim($value), 'slug'=>sanitize_title_with_dashes(trim($value)), 'parent'=>0 ) ); $theCatId = $term['term_id']; } else { $theCatId = $theCat->term_id; } if($theCatId){ array_push($post_categories, $theCatId); } } array_push($post_categories,2); sort($post_categories); $pattern = '/?([A-Za-z]+)/'; preg_match($pattern, substr($sku, 0), $matches); $size = (!empty($matches)) ? '-' . $matches[0] : NULL; $post_name = sanitize_title_with_dashes(trim(strtolower($name . $size))); switch($size) { case '-XS': $textSize = 'Extra Small'; break; case '-S': $textSize = 'Small'; break; case '-M': $textSize = 'Medium'; break; case '-L': $textSize = 'Large'; break; case '-XL': $textSize = 'Extra Large'; break; default: $textSize = NULL; } $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_name = %s', $post_name); $cID = $wpdb->get_var( $query ); ///var_dump($cID); $postData = array( ///'post_content' => $value['ShortDescription'], // The full text of the post. 'post_name' => $post_name, // The name (slug) for your post 'post_title' => $name . ' ' . $textSize, // The title of your post. 'post_status' => 'publish', // Default 'draft'. 'post_type' => 'product', // Default 'post'. 'post_author' => 1, // The user ID number of the author. Default is the current user ID. 'ping_status' => 'closed', // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'. ///'post_parent' => [ <post ID> ] // Sets the parent of the new post, if any. Default 0. ///'menu_order' => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0. ///'to_ping' => // Space or carriage return-separated list of URLs to ping. Default empty string. ///'pinged' => // Space or carriage return-separated list of URLs that have been pinged. Default empty string. ////'post_password' => [ <string> ] // Password for post, if any. Default empty string. ///'guid' => // Skip this and let WordPress handle it, usually. ///'post_content_filtered' => // Skip this and let WordPress handle it, usually. 'post_excerpt' => $shortDescription, // For all your post excerpt needs. 'post_date' => date('Y-m-d H:i:s'), // The time post was made. 'post_date_gmt' => date('Y-m-d H:i:s'), // The time post was made, in GMT. 'comment_status' => 'open', // Default is the option 'default_comment_status', or 'closed'. ///'post_category' => array($post_categories) // Default empty. --- DOESN'T WORK ///'tags_input' => [ '<tag>, <tag>, ...' | array ] // Default empty. /// 'tax_input' => array($post_categories) // For custom taxonomies. Default empty. --- DOESN'T WORK ///'page_template' => [ <string> ] // Requires name of template file, eg template.php. Default empty. ); if (empty($cID) ) { ///insert data $cID = wp_insert_post($postData); //// Update the other meta data update_post_meta($cID, 'total_sales', '0'); /// If new //// Handle files from folder and attach to the product // The ID of the post this attachment is for. $parent_post_id = $cID; // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); // Check the type of tile. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype( $wp_upload_dir['baseurl'] . '/AllProducts/' . basename( $filename ), NULL ); //// check file exists and process $theFile = trim($wp_upload_dir['basedir'] . '/AllProducts/' . basename( $filename )); if(file_exists($theFile)){ // Prepare an array of post data for the attachment. $attachment = array( 'guid' => trim($wp_upload_dir['baseurl'] . '/AllProducts/' . basename( $filename ) ), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit' ); // Insert the attachment. $attach_id = wp_insert_attachment( $attachment, trim($wp_upload_dir['baseurl'] . '/AllProducts/' . basename( $filename )), $parent_post_id ); // Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $attach_id, $theFile ); wp_update_attachment_metadata( $attach_id, $attach_data ); // Finally! set our post thumbnail update_post_meta( $parent_post_id, '_thumbnail_id', $attach_id ); } } else { ///update data $postData = array('ID'=>$cID) + $postData; var_dump($postData); wp_insert_post($postData); } /// attach the product categories var_dump(array( $cID, $post_categories, 'product_cat')); echo ' '; ///wp_set_object_terms( $cID, $post_categories, 'product_cat', true); --- DOESN't WORK //wp_set_post_categories( $cID, $post_categories, false); --- DOESN't WORK wp_set_post_terms( $cID, $post_categories); //// --- DOESN't WORK ///wp_set_post_terms( $cID, array($post_categories), 'product_cat' ); --- DOESN't WORK //// Update the other meta data update_post_meta($cID, '_visibility', 'visible'); if($stock > 0) { update_post_meta($cID, '_stock_status', 'instock'); } else { update_post_meta($cID, '_stock_status', 'outofstock'); } update_post_meta($cID, '_downloadable', 'no'); update_post_meta($cID, '_virtual', 'no'); update_post_meta($cID, '_regular_price', $price); update_post_meta($cID, '_sale_price', ''); update_post_meta($cID, '_purchase_note', ''); update_post_meta($cID, '_featured', 'no'); update_post_meta($cID, '_weight', ''); update_post_meta($cID, '_length', ''); update_post_meta($cID, '_width', ''); update_post_meta($cID, '_height', ''); update_post_meta($cID, '_sku', $sku); update_post_meta($cID, '_product_attributes', 'a:0:{}'); update_post_meta($cID, '_sale_price_dates_from', ''); update_post_meta($cID, '_sale_price_dates_to', ''); update_post_meta($cID, '_price', $price); update_post_meta($cID, '_sold_individually', 'no'); update_post_meta($cID, '_pack_qty', $packQty); update_post_meta($cID, '_manage_stock', 'yes'); update_post_meta($cID, '_backorders', 'no'); update_post_meta($cID, '_stock', $stock); update_post_meta($cID, '_product_image_gallery', ''); $count++; if($count > 10) { break; } } //// End foreach
- The topic ‘How to assign new array of custom taxonomy to newly inserted post in a cron job’ is closed to new replies.