How can I programmatically create a connection between one custom post type, cpt, (with post id known) to another on cpt on publish?
我正在使用VoodooPress\'s front-end posting method 发布名为“post-type-a”的帖子类型。post-type-A表单中的一个输入字段是public inventory number,它通过一些wp\\U查询love为我提供了我希望与之建立关系的post-type-B的post id。
我知道我可以this 函数使用自定义字段创建从post-type-a到post-type-B的单向连接。
add_action(\'publish_page\', \'add_custom_field_automatically\');
add_action(\'publish_post\', \'add_custom_field_automatically\');
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, \'field-name\', \'custom value\', true);
}
}
但是如何使用@Scribu的编程创建连接呢
Posts 2 Posts 插件?双向关系将减少许多麻烦和编程时间。:)
作为参考,下面的代码段是connect api reference 对于插件。。。
/**
* Connect a post to another one
*
* @param int $post_a The first end of the connection
* @param int $post_b The second end of the connection
* @param bool $bydirectional Wether the connection should be bydirectional
*/
function p2p_connect( $post_a, $post_b, $bydirectional = false ) {
add_post_meta( $post_a, P2P_META_KEY, $post_b );
if ( $bydirectional )
add_post_meta( $post_b, P2P_META_KEY, $post_a );
}