$30
Programmatically create connection with Posts 2 Posts on publish?
I am using VoodooPress's front-end posting method to publish a post type called post-type-A. One input field in the post-type-A form is the public inventory number, which through some wp_query love gives me the post id of the post-type-B that I wish to create a relationship with.
I know that I can use this function to create a one-way connection from post-type-A to post-type-B using a custom field.
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);
}
}But how do I programmatically create a connection using @Scribu's Posts 2 Posts plugin? A two-way relationship would reduce a lot of hassle and programming time.
I have already created a function to relate the two in the backend.
For reference, the snippet below is the connect api reference for the plugin...
/**
* 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 );
}
Matt Taylor | 06/21/11 at 4:42pm
| Edit
(3) Possible Answers Submitted...
-

Last edited:
06/21/11
4:45pm -

Last edited:
06/21/11
4:46pmKailey Lampert says:Try this - editing the post type name as needed:
add_action( 'init', 'build_connections' );
function build_connections() {
if (function_exists('p2p_register_connection_type')) {
p2p_register_connection_type( array(
'from' => 'post_type_a',
'to' => 'post_type_b',
'reciprocal' => true,
) );
}
}
- 06/21/11 4:48pm
Matt Taylor says:I have already created a function to relate the two in the backend.
Hi Kailey, I'm looking to create the actually post to post relationship, not the general connection. - 06/21/11 4:49pm
Kailey Lampert says:ah - sorry, misread/understood
- 06/21/11 4:48pm
-

Last edited:
06/21/11
5:25pmUtkarsh Kukreti says:What code do you use to get post B's id? When you have both the ids, you can do something like
<?php
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)) {
$stocknum = $_GET['stocknum'];
$posts = get_posts(array(
'post_type' => 'inventory',
'post_status' => 'publish',
'meta_key' => '_stocknum',
'meta_value' => $stocknum,
));
if(is_array($posts) && count($posts) > 0) {
$post_b = $posts[0];
p2p_connect($post_ID, $post_b->ID);
}
}
}
Previous versions of this answer: 06/21/11 at 5:25pm
- 06/21/11 4:49pm
Matt Taylor says:Hi Utkarsh, this is the code I am using to get the post ID...
// pull stock number from url
global $post;
$stocknum = $_GET['stocknum'];
$referringinventory = new WP_Query( array(
'post_type' => 'inventory',
'post_status' => 'publish',
'meta_key' => '_stocknum',
'meta_value' => $stocknum,
) );
if( $referringinventory->have_posts() ) :
while( $referringinventory->have_posts() )
$referringinventory->the_post();
$referringinventory_postid = $post->ID;
endif;
wp_reset_query(); - 06/21/11 4:50pm
Matt Taylor says:I forgot to mention that stock numbers are unique.
- 06/21/11 4:53pm
Utkarsh Kukreti says:What are you using $_GET['stocknum'] for here? That wouldn't be present when publish_post is fired.
- 06/21/11 5:00pm
Matt Taylor says:I'm extracting stocknum from the url.
example: /financing?stocknum=3223 - 06/21/11 5:01pm
Matt Taylor says:Would it be helpful if I linked to the full code?
- 06/21/11 5:12pm
Utkarsh Kukreti says:It may be.
Is there any direct way which can find the other kind of post, from the details of the first post? (based on other custom fields, title, id or anything) - 06/21/11 5:21pm
Matt Taylor says:The source code for the post-type-A page is here: https://gist.github.com/9deef3193e19e1a9dc12
What type of other details are you looking for? I'm not following you. :) - 06/21/11 5:25pm
Utkarsh Kukreti says:Ah, you are inserting the post from a custom page. Please try out the edited code in my main post.
- 06/22/11 7:59am
Matt Taylor says:The snippet as you provided does not work. After reviewing your snippet again I noticed two things, though I'm not sure what, if any impact they have...
1) You used an action hook of publish_post where the front-end submit plugin uses wp_insert_post.
2) The check is wrapped around wp_is_post_revision. This is a new post, not a revision. The frond-end post form script I'm using (which is a fairly typical one) redirects to the newly created page once the user clicks "save". Obviously the post being created does not have an ID number.
So the question becomes what is the best way to get the post id of post-type-A?
I see two options:
a) Use an call-back hook of some kind to get the post number and then generate the connection.
b) Since the user who submitted the post is automatically redirected to the new page, include in the newly re-created page's theme template a snippet that creates the connection on page load.
- 06/22/11 8:57am
Utkarsh Kukreti says:I'm thinking of just adding this code into the creditappform.php code that you have posted on gist. Could you try this out? https://gist.github.com/fac8794395d3125ede37
- 06/22/11 10:04am
Matt Taylor says:That code works, thanks.
One point of clarification if you don't mind...
# create connection
p2p_connect($pid, $referringinventory_postid);
Should $referringinventory_postid be first if the connect is going from the credit app to the piece of inventory? - 06/22/11 10:10am
Utkarsh Kukreti says:p2p_connect($pid, $referringinventory_postid); will create a connection from the inserted post to the inventory post, so if you reverse the arguments, the connection will be from inventory to the new post.
- 06/22/11 10:10am
Utkarsh Kukreti says:The arguments are $from, $to as mentioned here
- 06/22/11 10:13am
Matt Taylor says:Thanks for everything Utkarsh!
- 06/21/11 4:49pm
This question has expired.
Matt Taylor voted on this question.
Current status of this question: Completed




