Skip Navigation

[Resolved] Create post relationship using PHP

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to connect two posts using PHP and a Post Relationship inside a custom REST API route.

Solution: Use the toolset_connect_posts API to connect two posts programmatically.

Relevant Documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 2 voices.

Last updated by avansisI-2 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1864859

Good afternoon,

I'm doing a synchronization with the wordpress API, and I need to insert the relationship into a custom type from an application, but I don't know which field I have to refer to.

my code is this


//Crear bono

add_action( 'rest_api_init', function () {
 // register_rest_route( 'bono/v1', '/user/(?<userid>\d+)', array(
 register_rest_route( 'bono/v1', '/userid/(?<userid>[\d]+)/campana/(?<campana>[\d]+)', array(
    'methods' => 'GET',
    'callback' => 'crea_bono'
  ));
});
function crea_bono($request) {
$userid= $request['userid'];

$campana= $request['campana'];

$pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
 $max = strlen($pattern)-1;
 $code_gen=generate_string($pattern, 20);
if($userid) {
    $bono = array(
    'post_type'    => 'bonos',
    'post_title'    => $code_gen,
    'post_content'  => '',
    'post_status'   => 'publish',
    'post_author'   => $userid
);

$postid=wp_insert_post( $bono );
add_post_meta( $postid, 'wpcf-referencia-del-bono', $code_gen, true);
$taxonomy = 'estado';
//$termObj  = get_term_by( 'id', 144, $taxonomy);
wp_set_object_terms($postid, 144, $taxonomy);

add_post_meta( $postid, 'campana-bonos-bono', $campana, true);

//echo $postid;
//print_r($bono);
}
   
    $response = new WP_REST_Response($userid);
    $response->set_status(200);

    return $response;
}
#1865041

Hello, it looks like you want to create a relationship between two posts using PHP. Types post relationships are not stored as postmeta field values, they are stored in custom database tables. Therefore, we recommend you use the post relationships API toolset_connect_posts to create relationships programmatically. We have documentation and an example available here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Click "+More Usage examples" to see the code example showing how to connect two posts in a post relationship using the post relationship slug and the IDs of both posts.

#1865095

Could you help me with my code?

My relationships is 'campana-bonos-bono'

Thanks 🙂

#1866803

Sure, show me the code you have written and I will provide feedback.

#1866811

If i add code example

// Connect a custom Author post with the ID of "5" to a custom Book post with the ID of "7" in a post relationship between Books and Authors
toolset_connect_posts( 'book-author', 5, 7 );

or

//relación bonos-campaña
//
toolset_connect_campana-bonos( 'campana', 5, 7 );

It's happen

Your PHP code changes were rolled back due to an error on line 403 of file wp-content/themes/mytheme/functions.php. Please fix and try saving again.

Uncaught Error: Call to undefined function toolset_connect_posts() in wp-content/themes/mytheme/functions.php:403
Stack trace:
#0 wp-settings.php(514): include()
#1 wp-config.php(102): require_once('/var/www/wordpr...')
#2 wp-load.php(37): require_once('/var/www/wordpr...')
#3 wp-admin/admin.php(34): require_once('/var/www/wordpr...')
#4 wp-admin/theme-editor.php(10): require_once('/var/www/wordpr...')
#5 {main}
thrown

#1866893

- Where did you add the toolset_connect_posts code? Is it inside the crea_bono function, or somewhere else?
- For testing, please tell me the ID of one Campana post and the ID of one Bonos post you want to connect with post relationships.

#1866935

ID of one bonos : 7624

ID of one Campana: 5168

#1867057

- Where did you add the toolset_connect_posts code? Is it inside the crea_bono function, or somewhere else? Please explain.

#1867063

this was my first try but you told me i had to add it as toolset_connect_posts but when i add that function it gives me error so it doesn't appear in my code

//Crear bono

add_action( 'rest_api_init', function () {
 // register_rest_route( 'bono/v1', '/user/(?<userid>\d+)', array(
 register_rest_route( 'bono/v1', '/userid/(?<userid>[\d]+)/campana/(?<campana>[\d]+)', array(
    'methods' => 'GET',
    'callback' => 'crea_bono'
  ));
});
function crea_bono($request) {
$userid= $request['userid'];

$campana= $request['campana'];

$pattern = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 $max = strlen($pattern)-1;
 $code_gen=generate_string($pattern, 10);
if($userid) {
    $bono = array(
    'post_type'    => 'bonos',
    'post_title'    => $code_gen,
    'post_content'  => '',
    'post_status'   => 'publish',
    'post_author'   => $userid
);

$postid=wp_insert_post( $bono );
add_post_meta( $postid, 'wpcf-referencia-del-bono', $code_gen, true);
$taxonomy = 'estado';
//$termObj  = get_term_by( 'id', 144, $taxonomy);
wp_set_object_terms($postid, 144, $taxonomy);

add_post_meta( $postid, 'campana-bonos-bono', $campana, true);

//echo $postid;
//print_r($bono);
}
   
    $response = new WP_REST_Response($userid);
    $response->set_status(200);

    return $response;
}
#1867153

Try it like this inside your existing crea_bono function, just after the wp_set_object_terms line. I commented out the add_post_meta line, since it is not necessary to create a relationship:

//Crear bono
 
add_action( 'rest_api_init', function () {
 // register_rest_route( 'bono/v1', '/user/(?<userid>\d+)', array(
 register_rest_route( 'bono/v1', '/userid/(?<userid>[\d]+)/campana/(?<campana>[\d]+)', array(
    'methods' => 'GET',
    'callback' => 'crea_bono'
  ));
});
function crea_bono($request) {
$userid= $request['userid'];
 
$campana= $request['campana'];
 
$pattern = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 $max = strlen($pattern)-1;
 $code_gen=generate_string($pattern, 10);
if($userid) {
    $bono = array(
    'post_type'    => 'bonos',
    'post_title'    => $code_gen,
    'post_content'  => '',
    'post_status'   => 'publish',
    'post_author'   => $userid
);
 
$postid=wp_insert_post( $bono );
add_post_meta( $postid, 'wpcf-referencia-del-bono', $code_gen, true);
$taxonomy = 'estado';
//$termObj  = get_term_by( 'id', 144, $taxonomy);
wp_set_object_terms($postid, 144, $taxonomy);
toolset_connect_posts( 'campana-bonos-bono', 5168, 7624 );
//add_post_meta( $postid, 'campana-bonos-bono', $campana, true);
 
//echo $postid;
//print_r($bono);
}
    
    $response = new WP_REST_Response($userid);
    $response->set_status(200);
 
    return $response;
}

Then check the Campana Bonos post 5168 to see if the Bonos post 7624 is related correctly.

#1867155

Thanks Christian, the campaign and the bonus you comment on have been linked correctly, but I need this to be dynamic.

I have a url that generates the voucher from an APP with this address

/ wp-json / bonus / v1 / userid / 1 / campana/ 1

where the bell / 1 is substitutable by the campaign number. In this case the 5168 and the userid / 1 / for the user id. Therefore I need this to be dynamic, and bring it to me referenced in the url.

#1867683

My issue is resolved now. Thank you!