Skip Navigation

[Resolved] toolset_connect_posts is not working as intended

This support ticket is created 3 years, 4 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Waqar 3 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2465867

Tell us what you are trying to do?

I have a Gravity Form that uses the Advanced Post Creation addon to create 2 posts of different types on submission. First, it creates a Theme (theme-post) post type post, and then it creates a Practice Criteria (practice-criteria) post type post.

I have a one-to-many relationship between the two post types (slug is theme-practice-criteria-relationship).

I am using a Gravity Forms hook for when the advanced post creation is done for both posts to create a toolset relationship between the two posts.

Here is the code I am using:

add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_toolset_setparent', 10, 4 );

$post_id_of_theme = '';

function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {
  
  GFCommon::log_debug( 'Feed ID => ' . $feed['id'] );
  
  if ($feed['id'] == 5) {
    $GLOBALS['post_id_of_theme'] = $post_id;
    GFCommon::log_debug( 'Post ID of Theme (Feed 5) => ' . $GLOBALS['post_id_of_theme'] );
    
  }
  
  if ($feed['id'] == 1) {
    GFCommon::log_debug( 'Field Type => ' . rgar( $entry, '27' ) );
    if (rgar( $entry, '27' ) == "1") {
      $theme_id = intval(rgar ($entry, '28'));
      $theme_post = get_post($theme_id);
      $criteria_post = get_post($post_id);
      GFCommon::log_debug( 'Theme Post ID in Field 28 => ' . $theme_id);
      GFCommon::log_debug('Criteria Post ID => '. $post_id);
      toolset_connect_posts('theme-practice-criteria-relationship', $theme_id, $post_id );
  // toolset_connect_posts('theme-practice-criteria-relationship', $theme_post, $criteria_post );
      GFCommon::log_debug('Post connection code has run');
    }
    else {    
    $theme_id = intval($GLOBALS['post_id_of_theme']);
      $theme_post = get_post($theme_id);
      $criteria_post = get_post($post_id);
    GFCommon::log_debug( 'Post ID of Theme (Global) => ' . $GLOBALS['post_id_of_theme'] );
    GFCommon::log_debug( 'Post ID of Theme (Local) => ' . $theme_id );
    GFCommon::log_debug( 'Post ID of Practice Criteria => ' . $post_id );
	toolset_connect_posts('theme-practice-criteria-relationship', $theme_post, $criteria_post );
  	}
  }
 
}

I don't see any errors, but the Gravity Forms log shows the correct theme_id and post_id but hangs at the toolset_connect_posts() function (none of the code after that executes.

I even ran a simple version where I just use the following code:

 
add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_toolset_setparent', 10, 4 );

function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {

toolset_connect_posts('theme-practice-criteria-relationship', 166, $post_id );
 
}

Even this doesn't work.

My site is on PHP 7.4, hosted with WPEngine.

All code has been added to Toolset's Custom Code section and is active.

#2465907

The error I am getting is:

Fatal error: Uncaught InvalidArgumentException: The element to connect with doesn't belong to the relationship definition provided. in /nas/content/live/myconscious/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/DatabaseLayer/PotentialAssociation/PostQuery.php:104 Stack trace: #0 /nas/content/live/myconscious/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/DatabaseLayer/DatabaseLayerFactory.php(187): OTGS\Toolset\Common\Relationships\DatabaseLayer\PotentialAssociation\PostQuery->__construct(Object(Toolset_Relationship_Definition), Object(Toolset_Relationship_Role_Child), Object(Toolset_Post), Array, Object(OTGS\Toolset\Common\Relationships\DatabaseLayer\DatabaseLayerFactory)) #1 /nas/content/live/myconscious/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/driver.php(39): OTGS\Toolset\Common\Relationships\DatabaseLayer\DatabaseLayerFactory->potential_association_query(Object(Toolset_Relationship_Definition), Object(Toolset_Relationship_Role_Child), Object(Toolset_Post)) #2 /nas in /nas/content/live/myconscious/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/DatabaseLayer/PotentialAssociation/PostQuery.php on line 104

I have triple checked and the post types are correct. I am not sure why this is happening.

#2466353

Hi,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot this, I'll suggest the following steps:

1. First please determine, exactly which of the two instances of the "toolset_connect_posts" is producing the error.

2. Next, ensure that the 3 arguments being passed to that instance of the function are correct:

- the first one needs to be the target relationship slug.
- the second one needs to be the ID of the parent post that needs to be connected.
(the parent post type in the relationship is the one on the left-hand side of the relationship screen)
- the third one needs to be the ID of the child post that needs to be connected.
(the child post type in the relationship is the one on the right-hand side of the relationship screen)

3. If the relationship is of type 'one-to-one' or 'one-to-many', please also see if the two posts that you're trying to connect are already connected to some other post(s) or not.
(i.e. the two posts should be allowed to be connected by the relationship type rule)

4. It would also be interesting to also test your code snippet from the active theme's "functions.php" file, instead of the Toolset's custom code feature. Sometimes the order of code execution can also matter, especially when third-party plugin code is also involved.

I hope these suggestions would help and please let me know how it goes.

regards,
Waqar