Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Programmatically create connection with Posts 2 Posts on publish? WordPress

  • SOLVED

How can I programmatically create a connection between one custom post type (with post id known) to another on cpt on publish?

I am using [[LINK href="http://voodoopress.com/2011/03/add-a-meta-box-to-use-custom-fields-on-front-end-post-form/"]]VoodooPress's front-end posting method[[/LINK]] 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 [[LINK href="http://wpcanyon.com/tipsandtricks/adding-a-custom-field-automatically-on-postpage-publish/"]]this function[[/LINK]] 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 [[LINK href="http://scribu.net/wordpress/posts-to-posts"]]Posts 2 Posts[[/LINK]] 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 [[LINK href="http://plugins.trac.wordpress.org/browser/posts-to-posts/tags/0.2/api.php#L41"]]connect api reference[[/LINK]] 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 );
}

Answers (3)

2011-06-21

Utkarsh Kukreti answers:

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);
}
}
}


Matt Taylor comments:

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();


Matt Taylor comments:

I forgot to mention that stock numbers are unique.


Utkarsh Kukreti comments:

What are you using $_GET['stocknum'] for here? That wouldn't be present when publish_post is fired.


Matt Taylor comments:

I'm extracting stocknum from the url.

example: /financing?stocknum=3223


Matt Taylor comments:

Would it be helpful if I linked to the full code?


Utkarsh Kukreti comments:

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)


Matt Taylor comments:

The source code for the post-type-A page is here: [[LINK href="https://gist.github.com/9deef3193e19e1a9dc12"]]https://gist.github.com/9deef3193e19e1a9dc12[[/LINK]]

What type of other details are you looking for? I'm not following you. :)


Utkarsh Kukreti comments:

Ah, you are inserting the post from a custom page. Please try out the edited code in my main post.


Matt Taylor comments:

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 <em>publish_post</em> where the front-end submit plugin uses <em>wp_insert_post</em>.

2) The check is wrapped around <em>wp_is_post_revision</em>. 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 <em>post-type-A</em>?

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.


Utkarsh Kukreti comments:

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


Matt Taylor comments:

That code works, thanks.

One point of clarification if you don't mind...

# create connection
p2p_connect($pid, $referringinventory_postid);


Should <em>$referringinventory_postid</em> be first if the connect is going from the <em>credit app</em> to the piece of <em>inventory</em>?


Utkarsh Kukreti comments:

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.


Utkarsh Kukreti comments:

The arguments are $from, $to as mentioned [[LINK href="http://plugins.trac.wordpress.org/browser/posts-to-posts/trunk/api.php#L45"]]here[[/LINK]]


Matt Taylor comments:

Thanks for everything Utkarsh!

2011-06-21

Julio Potier answers:

Yes you can, let me try, i'll post you the code !

2011-06-21

Kailey Lampert answers:

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,
) );

}
}


Matt Taylor comments:

<blockquote>I have already created a function to relate the two in the backend.
</blockquote>

Hi Kailey, I'm looking to create the actually post to post relationship, not the general connection.


Kailey Lampert comments:

ah - sorry, misread/understood