我有一个自定义的帖子类型“Product”,希望在创建新产品时自动创建一个帖子类别“Product Name”,这样帖子类别(非自定义帖子类别)将与新产品具有相同的名称,并且所有相关帖子都可以链接到自定义帖子类型。
我曾试图链接到wp\\u publish\\u帖子,但显然它已重命名为{$new_status}_{$post->post_type}
所以我想标签应该是publish\\u product?
// adds category with name of product automatically
add_filter( \'publish_product\', \'my_publish_product\' );
function my_publish_product( $post ) {
global $wpdb;
$post_type = \'product\';
if ( ! $post = get_post( $post ) )
return;
if ( \'publish\' == $post->post_status )
return;
$wpdb->update( $wpdb->posts, array( \'post_status\' => \'publish\' ), array( \'ID\' => $post->ID ) );
clean_post_cache( $post->ID );
$old_status = $post->post_status;
$post->post_status = \'publish\';
wp_transition_post_status( \'publish\', $old_status, $post );
/** This action is documented in wp-includes/post.php */
do_action( \'edit_post\', $post->ID, $post );
/** This action is documented in wp-includes/post.php */
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
/** This action is documented in wp-includes/post.php */
do_action( \'save_post\', $post->ID, $post, true );
/** This action is documented in wp-includes/post.php */
do_action( \'wp_insert_post\', $post->ID, $post, true );
if ( \'post\' != $post_type ) {
return;
} else {
add_action( \'wp_insert_post\', \'my_wp_insert_post($post->post_name)\' );
return;
}
}
function my_wp_insert_post( $post_name )
{
$my_cat = array(
\'cat_ID\' => 0,
\'cat_name\' => $post_name,
\'category_parent\' => \'products\',
\'taxonomy\' => \'category\'
);
wp_insert_category($my_cat);
}
我不太擅长使用钩子和过滤器,所以如果这一切都错了或没有意义,我深表歉意。