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;
}
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.
Could you help me with my code?
My relationships is 'campana-bonos-bono'
Thanks 🙂
Sure, show me the code you have written and I will provide feedback.
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
- 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.
- Where did you add the toolset_connect_posts code? Is it inside the crea_bono function, or somewhere else? Please explain.
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;
}
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.
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.
My issue is resolved now. Thank you!