Post Relationships API
Toolset Types provides a Post Relationship API allowing you to use custom PHP for querying related content in a convenient way. This page explains how to use this API and provides code examples.
This action is executed just after creating a new association between two posts. Action will be executed when creating a new related content or connecting to an existing one.
$relationship_slug
. string: Relationship slug.
$parent_id
. int: Post ID of the parent element of the relationship.
$child_id
. int: Post ID of the child element of the relationship.
$intermediary_id
. int: Post ID of the intermediary element of the relationship.
$association_id
. int: ID of the new association created.
More Usage examples
// Refreshing WP Super Cache cache for both parent and child posts, in case you have views displaying related content. add_action( 'toolset_association_created', 'refresh_cache_after_association_created', 10, 5 ); function refresh_cache_after_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) { wpsc_delete_post_cache( $parent_id ); wpsc_delete_post_cache( $child_id ); }
This action is executed just before deleting a new association between two posts.
$relationship_slug
. string: Relationship slug.
$parent_id
. int: Post ID of the parent element of the relationship.
$child_id
. int: Post ID of the child element of the relationship.
$intermediary_id
. int: Post ID of the intermediary element of the relationship.
$association_id
. int: ID of the new association created.
More Usage examples
// When an association between two posts gets deleted, add a log record using a custom function myplugin_add_log_entry() add_action( 'toolset_before_association_delete', 'myplugin_log_association_deleted', 10, 5 ); function myplugin_log_association_deleted( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_uid ) { myplugin_add_log_entry( "Deleted an association #{$association_uid} in a relationship '{$relationship_slug}' between posts #{$parent_id} and #{$child_id} (with intermediary #{$intermediary_id}" ). }
Connects posts within a given post relationship.
string|string[] $relationship
The slug of the relationship or an array with the parent and the child post type of a legacy relationship.
int|WP_Post $parent
The parent post to connect.
int|WP_Post $child
The child post to connect.
int|WP_Post|null $intermediary
Optional. The intermediary post to use for a many-to-many post relationship. If it is not provided and it is needed, a new post will be created.
Returns an array with some of the following elements:
bool 'success'
: This element is always present. true
or false
.string 'message'
: A message describing the result. May not be always present.int 'intermediary_post'
: This element is present only if the operation has succeeded and contains the ID of the newly created intermediary post or zero if there is none.More Usage examples
// 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 );
Disconnects posts within a given post relationship.
$relationship
- string or array of strings Slug of the relationship or an array with the parent and the child post type of a legacy relationship.
$parent
- integer Post ID of the parent post to connect.
$child
- integer Post ID of the child post to connect.
Returns true
or false
.
More Usage examples
// Disconnect a custom Author post with the ID of "5" from a custom Book post with the ID of "7" in a post relationship between Books and Authors toolset_disconnect_posts( 'book-author', 5, 7 );
Retrieves an ID of the parent post, using a legacy post relationship. Or, if new post relationships are already enabled, relationships migrated from the legacy implementation. For this to work, there needs to be a relationship between $target_type
and the provided post’s type. Note: For more complex cases, use toolset_get_related_post() or toolset_get_related_posts() functions.
$post
- WP_Post|int Post whose parent should be returned.
$target_type
- string Parent post type.
int – Post ID or zero if no related post was found.
More Usage examples
$book = get_post( $book_id ); $writer = toolset_get_parent_post_by_type( $book, 'writer' );
Get extended information about a single relationship definition.
$identification
- Relationship slug or a pair of post type slugs identifying a legacy relationship.
array|null - Relationship information or null if it doesn't exist.
The relationship array contains following elements:
array( 'slug' (only if m2m is enabled) => Unique slug identifying the relationship. 'labels' (only if m2m is enabled) => array( 'plural' => Plural display name of the relationship. 'singular' => Singular display name of the relationship. ), 'roles' => array( 'parent' => array( 'domain' => Domain of parent elements. Currently, only 'posts' is supported. 'types' => Array of (post) types involved in a single relationship. Currently, there's always only a single post type, but that may change in the future. ), 'child' => Analogic to the parent role information. 'intermediary' (present only if m2m is enabled and if the relationship is of the many-to-many type) => Analogic to the parent role information. The domain is always 'posts' and there is always a single post type. ), 'cardinality' => array( 'type' => 'many-to-many'|'one-to-many'|'one-to-one', 'limits' => array( 'parent' => array( 'min' => The minimal amount of connected parent ("left side") posts for each child ("right side") post. Currently, this is always 0, but it may change in the future. 'max' => The maximal amount of connected parent posts for each child post. If there is no limit, it's represented by the value -1. ), 'child' => Analogic to the parent role information. ) ), 'origin' => 'post_reference_field'|'repeatable_group'|'standard' How was the relationship created. "standard" is the standard one. )
More Usage examples
$relationship = toolset_get_relationship('tracks-of-an-album');