Support Forum
Support › MB Relationships › Excluding the current post in reciprocal relationshipsResolved
Hi,
I am building a multi-user website and I am a little worried that it is possible to create a relationship between a post and ...the same post. It's kind of like saying that I am my own brother, but it doesn't make sense because I have no brothers.
It wouldn't bother me on sites where I am the only user creating content, but on a multi-user website, I am very afraid that my dear users will create a mess as time goes by 🙂
Is there a way to disable the option to select the current post? Maybe in query args?
Greetings,
Tom
Hello,
The setting reciprocal
of the relationship helps you to prevent connecting the current post to itself. Please read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#relationship-settings
But how does it prevent connecting to the current post? In the post edit screen, I can connect to the current post regardless of the "Reciprocal relationship" checkbox status. It doesn't seem to matter.
The documentation doesn't say anything about connecting to the current post in the context of reciprocal relationships...
Hell Tom,
I see the issue. In this case, you will need to use the code to register the relationship and use the query args to exclude the current post from the list of posts.
add_action( 'mb_relationships_init', 'callback_function' );
function callback_function() {
$post_id = null;
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = intval( $_POST['post_ID'] );
}
MB_Relationships_API::register( [
'id' => 'post-to-post',
'reciprocal' => true,
'from' => [
'object_type' => 'post',
'post_type' => 'post',
'meta_box' => [
'context' => 'normal',
],
],
'to' => [
'object_type' => 'post',
'post_type' => 'post',
'meta_box' => [
'context' => 'normal',
],
'field' => [
'query_args' => [
'post__not_in' => [ $post_id ], // here
],
],
],
] );
}
Thank you, Peter. It works.
Greetings,
Tom