发布新产品时在wordpress中创建新帖子

时间:2019-06-03 14:33:05

标签: wordpress woocommerce hook

我想在Woocommerce中创建新产品时在Wordpress中创建自动发布。

我想使用产品的标题和该产品的链接。

因此该函数读取了“该产品的标题”,该标题成为了文章的标题(帖子) “此产品的链接”成为帖子的“内容”(在帖子中创建快照)

但是,只有在产品发布并且“活动”时,才必须发布此帖子。如果产品未发布,则该帖子也不会发布。

更好的方法是,对产品的所有修改都不会创建新帖子,而是会更新相关的帖子...

有人可以帮我吗?预先感谢

有什么办法吗?不好吗?

2 个答案:

答案 0 :(得分:0)

您可以使用此函数add_this_to_new_products这个函数在创建新产品时运行,并使用wp_insert_post完成以创建新帖子(包含您的数据)。

function add_this_to_new_products( $new_status, $old_status, $post ) {

global $post;

if ( $post->post_type !== 'product' ) return;

if ( 'publish' !== $new_status or 'publish' === $old_status ) return;

 $post = array( 
    'ID'             => [ <post id> ] 
    'post_content'   => [ <string> ] 
    'post_name'      => [ <string> ] 
    'post_title'     => [ <string> ] 
    'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] 
    'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] 
    'post_author'    => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
    'ping_status'    => [ 'closed' | 'open' ] // 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'   => [ <string> ] // For all your post excerpt needs.
    'post_date'      => [ Y-m-d H:i:s ] // The time post was made.
    'post_date_gmt'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
    'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
    'post_category'  => [ array(<category id>, ...) ] // Default empty.
    'tags_input'     => [ '<tag>, <tag>, ...' | array ] // Default empty.
    'tax_input'      => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
    'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
    );  

    wp_insert_post($new_post);

} 
add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );

让我知道。

答案 1 :(得分:0)


global $post;

if ( $post->post_type !== 'product' ) return;
// On récupère les données dont on a besoin pour créer le nouveau post  
// Get $product object from product ID

$product = wc_get_product( $product_id );

// Now you have access to (see above)...

//$product->get_type();
$product->get_name();
$nomduproduit = $product->get_name();
// On récupère l'URL du Jeu
$jeuurl = get_permalink( $item['product_id'] ) ;


if ( 'publish' !== $new_status or 'publish' === $old_status ) return;

$post = array( 
   //'ID'             => [ <post id> ] 
   'post_content'   => return '<a href="'. $jeuurl .'">'. $item_name .'</a>';  
   'post_name'      => [ '$nomduproduit' ] 
   'post_title'     => [ '$nomduproduit' ] 
   'post_status'    => [ 'publish' ] 
   'post_type'      => [ 'post' ] 
   'post_author'    => [ 'BingoHome' ] // The user ID number of the author. Default is the current user ID.
   'ping_status'    => [ 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
   'post_parent'    => [ '$product_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'   => [ <string> ] // For all your post excerpt needs.
   'post_date'      => [ Y-m-d H:i:s ] // The time post was made.
   'post_date_gmt'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
   'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
   'post_category'  => [ array(<category id>, ...) ] // Default empty.
   'tags_input'     => [ '' | array ] // Default empty.
   'tax_input'      => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
   'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
   );  

   wp_insert_post($new_post);

} ````   

What do you think ?