Skip Navigation

[Resolved] Problem with the latest plugin updates.

This support ticket is created 5 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1113660

I have a custom plugin where I get the Parent ID from a post relationship using this code.

$parent_id = toolset_get_related_post( $id, 'doctors-at-clinic', 'parent');

Prior to the recent plugin updates this code worked fine. But immediately after updating Types and Views plugins on 18th Sept both my site admin went blank, and so did the network admin. This error was the cause.

Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /dom22087/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php:223 Stack trace: #0 /dom22087/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php(158): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'child') #1 /dom22087/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(152): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct('bug', 'doctors-at-clin...', Array) #2 /dom22087/wp-content/plugins/my-directory-plugin/inc/cmb2/cmb2-fields.php(38): toolset_get_related_post('bug', 'doctors-at-clin...', 'parent') #3 /wordpress-4.9.8/wp-includes/class-wp-hook.php(286): listing_cmb2_metabox('') #4 /wordpress-4.9.8/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #5 /wordpress-4.9.8/wp-includes/plugin.ph in /dom22087/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 223

I am able to partially fix it with adding these lines to the code.

if (!empty ($id) ) {  
$parent_id = toolset_get_related_post( $id, 'doctors-at-clinic', 'parent'); // post parent relationship
}

My site admin does not break anymore, but the network admin still does.

Please let me know what the cause is. Something got changed in the latest updates that is causing this error.

Thanks

#1113904

Hi, it's hard for me to say without being able to see all the code. I need to know how $id is defined, and when this code is executed. Can I see all the relevant code here? Where/when is this code executed? Is this code added with a hook? Any more information you can provide will be helpful.

#1113913

Hi Christian,

The code is very simple. Its used to populate CMB2 fields with post meta info that already exists. This all works on the backend in the post edit screen.

function listing_cmb2_metabox() {
	
global $post, $pagenow, $typenow, $current_screen;

// Get Post ID from URL
$url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
$substring_start_pos = strpos($url, 'pid=') + 41; 
$url_number = substr($url, $substring_start_pos);
$id = rtrim($url_number,"&action=edit");

if (!empty ($id) ) {  
$parent_id = toolset_get_related_post( $id, 'doctors-at-clinic', 'parent'); // post parent relationship
}

if (!empty ($parent_id) ) {
$address = get_post_meta($parent_id, 'martygeocoderaddress', true);
}

}

add_action( 'cmb2_admin_init', 'listing_cmb2_metabox' );

I'm just getting an address from the post parent if there is one, and if the address exists.

#1113922

This is actually the expected behavior when the $id parameter is not a valid post ID or WP_Post object. From the documentation:

We always validate the inputs and if something in the input is completely wrong (e.g. wrong role name, argument type mismatch, etc.), we throw an InvalidArgumentException. That will give the developer an opportunity to immediately realize that something is wrong, opposed to failing quietly.

The first parameter in the toolset_get_related_post function call must be either an ID or a WP_Post, and it is required. If this code triggers a Fatal Error: InvalidArgumentException, that means you tried to call the function with an invalid first parameter. So I think you have a good solution in place now, where you test to be sure the ID variable is set before calling toolset_get_related_post.

add_action( 'cmb2_admin_init', 'listing_cmb2_metabox' );

I'm not familiar with this plugin, so I cannot say for sure if post relationships are available when cmb2_admin_init is triggered. See the documentation about timing here, in the "Remarks" section: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

To determine why the $id variable throws an InvalidArgumentException, you can add some debug information to your code:

// Get Post ID from URL
$url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
error_log('url: ' . $url);
$substring_start_pos = strpos($url, 'pid=') + 41; 
error_log('substring_start_pos: ' . $substring_start_pos);
$url_number = substr($url, $substring_start_pos);
error_log('url_number: ' . $url_number);
$id = rtrim($url_number,"&action=edit");
error_log('id: ' . $id);

Then remove the !empty check, test, and monitor the logs for more information. I'll be glad to take a look at those logs if the $id seems to be a valid post ID.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.