Excluding the current post in reciprocal relationships
Support › MB Relationships › Excluding the current post in reciprocal relationshipsResolved
- This topic has 5 replies, 2 voices, and was last updated 2 years, 8 months ago by
AMX.
-
AuthorPosts
-
March 16, 2023 at 10:57 PM #41057
AMX
ParticipantHi,
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,
TomMarch 17, 2023 at 10:06 PM #41067Peter
ModeratorHello,
The setting
reciprocalof 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-settingsMarch 17, 2023 at 11:50 PM #41072AMX
ParticipantBut 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.
March 17, 2023 at 11:54 PM #41073AMX
ParticipantThe documentation doesn't say anything about connecting to the current post in the context of reciprocal relationships...
March 18, 2023 at 6:38 PM #41087Peter
ModeratorHell 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 ], ], ], ] ); }April 7, 2023 at 3:49 AM #41373AMX
ParticipantThank you, Peter. It works.
Greetings,
Tom -
AuthorPosts
- You must be logged in to reply to this topic.