Skip Navigation

[Closed] Uncaught InvalidArgumentException

This support ticket is created 4 years, 3 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
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 1 reply, has 2 voices.

Last updated by Christian Cox 4 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1714877

This is the code in function.php

function my_custom_query2( $query, $widget ) {
$mixologue_id2 = get_post( get_the_ID() ); // Get ID of current mixologist
$cocktail_id2 = toolset_get_related_posts(
$mixologue_id2, // replace this with the child post id
'mixologist-cocktail', // slug of the post relationship
'parent', // get posts where 12345 is the child post ID
1000000, 0, // pagination
array( ),
'post_id',
'child'
);
$query->set( 'post__in', $cocktail_id2);
}
add_action( 'elementor/query/maquery2', 'my_custom_query2', 10, 2 );

Was working fine.
Then I got this email (after auto update of plugin?)

An error of type E_ERROR was caused in line 242 of the file /var/www/vhosts/romeosgin.com/httpdocs/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php. Error message: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /var/www/vhosts/romeosgin.com/httpdocs/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:242
Stack trace:
#0 /var/www/vhosts/romeosgin.com/httpdocs/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(173): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements()
#1 /var/www/vhosts/romeosgin.com/httpdocs/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(109): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct()
#2 /var/www/vhosts/romeosgin.com/httpdocs/wp-content/themes/romeos-child/functions.php(50): toolset_get_related_posts()
#3 /var/www/vhosts/romeosgin.com/httpdocs/wp-includes/class-wp-hook.php(287): my_custom_query2()

Yes I have read the other tickets related to that problem.
BUT your answer is always: post_id is not populated
But you do not say HOW to populate it...

I am using Elementor Pro
When going to template page Single Mixologist,
the post widget uses the query my_custom_query2 to show all cocktails by chosen mixologist.
So post_id is defined when you select a mixologist and go to Single page.

Despite the error showing, the page and query still work.

How to fix this problem?

#1715355

Hello, it's not clear to my why the post ID would be undefined, but maybe it's because the action is triggered somewhere else on the site besides the single mixologist page? A simple preventative solution would be to add some conditional that prevents the post relationship API from being queried if the post ID isn't defined for any reason:

function my_custom_query2( $query, $widget ) {
$mixologue_id2 = get_post( get_the_ID() ); // Get ID of current mixologist
if( !$mixologue_id2 ) {
  return;
}
$cocktail_id2 = toolset_get_related_posts(
$mixologue_id2, // replace this with the child post id
'mixologist-cocktail', // slug of the post relationship
'parent', // get posts where 12345 is the child post ID
1000000, 0, // pagination
array( ),
'post_id',
'child'
);
$query->set( 'post__in', $cocktail_id2);
}

Otherwise, the post ID may be available in the callback arguments of this function:

function my_custom_query2( $query, $widget ) {

You could use PHP logging to find out what exactly $query and $widget represent. The post ID may be buried in the data structure of one of those variables.

function my_custom_query2( $query, $widget ) {
error_log('query:');
error_log(print_r($query, true));
error_log('widget:');
error_log(print_r($widget, true));
...

Whenever the function executes, the log will show you the structure of data included in those two parameters. My guess is $query includes some information about the current post ID somewhere, but the structure of that information isn't clear without some logging. Elementor 's support team might be able to help you with that, since the action seems to be related to elementor:

add_action( 'elementor/query/maquery2', 'my_custom_query2', 10, 2 );

Their documentation would/should include information about the parameters available in a callback.

If you're not familiar with turning on logs, I can show you how to activate them in a typical wp-config.php file. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

If this causes an error, it's likely that your wp-config.php file already contains one of these definitions. Comment any duplicate definition out so the definition I provided is not duplicated to cause an error. Then save the wp-config.php file back to your server and load the page with the widget again. You should now find an error_log.txt file in your site's root directory. Use FTP to look for this file in the same directory as wp-config.php. You may need to click "Refresh" in your FTP browser to see a new file appear. Please download this file to your computer, open it in any text editing software, and review its contents. If it's not clear where to find the post ID, you can share the output with me in your next reply. Once that is done, you can revert the changes you made to wp-config.php and delete the log file using FTP.

The topic ‘[Closed] Uncaught InvalidArgumentException’ is closed to new replies.