【问题标题】:Create new post into wordpress when new product is published发布新产品时在 wordpress 中创建新帖子
【发布时间】:2019-06-03 14:33:05
【问题描述】:

当我在 Woocommerce 中创建新产品时,我想在 Wordpress 中创建自动帖子。

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

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

但只有在产品已发布且“活跃”时才必须发布此帖子。如果一个产品没有发布,那么帖子就没有发布。

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

有人可以帮我解决这个问题吗?提前致谢

有什么办法吗?不是很好吗?

【问题讨论】:

  • 您好 Jérôme,欢迎来到 Stack Overflow!很遗憾,我们无法为您提供完成此任务的分步说明。 Stack Overflow 不是按需编码服务。相反,请告诉我们您到目前为止(在编程方面)尝试了什么,我们将尽最大努力帮助您。另外,请花点时间来The Tour 了解这里可以接受什么样的问题。

标签: wordpress woocommerce hook


【解决方案1】:

您可以使用此函数 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 );

告诉我。

【讨论】:

  • 好的,告诉我。谢谢
【解决方案2】:

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 ? 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多