Skip Navigation

[Resolved] Toolset_get_related_posts gives backend error

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am writing a custom filter to work with contactform7 that displays a selection field for the dates on which a certain presentation is given. It works fine on the front end.

However, when I try to access this particular form on the back end WordPress is reporting a critical error.

Solution:

The client fixed it by solution of another thread:

https://toolset.com/forums/topic/problem-with-the-latest-plugin-updates/

Relevant Documentation:

This support ticket is created 3 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.

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
- 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by frederikB 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1911291

Tell us what you are trying to do?

I am writing a custom filter to work with contactform7 that displays a selection field for the dates on which a certain presentation is given. It works fine on the front end.

However, when I try to access this particular form on the back end WordPress is reporting a critical error. The error log gives this error:
Backend fatal error: PHP Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/deb96406/domains/autismedigitaal.nl/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246\nStack trace:\n#0 /home/deb96406/domains/autismedigitaal.nl/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\\Toolset\\Common\\Interop\\Commands\\RelatedPosts->set_query_by_elements(Array, 'child')\n#1 /home/deb96406/domains/autismedigitaal.nl/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\\Toolset\\Common\\Interop\\Commands\\RelatedPosts->__construct(false, 'agenda-presenta...', Array)\n#2 /home/deb96406/domains/autismedigitaal.nl/public_html/wp-content/themes/behteme-stichting/functions.php(29): toolset_get_related_posts(false, 'agenda-presenta...', Array)\n#3 /home/ in /home/deb96406/domains/autismedigitaal.nl/public_html/wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246\n, referer: hidden link

Here is my code:

function pine_dynamic_select_field_values ( $scanned_tag, $replace ) {

if ( $scanned_tag['name'] != 'datums' )
return $scanned_tag;

$presentatie_id = get_the_ID();
$relationship_slug = 'agenda-presentatie';

$rows = toolset_get_related_posts(
// get posts related to this one
$presentatie_id,

// Relationship between the posts
array( 'agenda', 'presentatie' ),

// Additional arguments
[
// Get posts where $writer is the parent in given relationship.
// This is mandatory because we're passing just a single $writer post as the first parameter.
'query_by_role' => 'child',

// pagination
'limit' => $posts_per_page,
'offset' => ( $current_page - 1 ) * $post_per_page,

'role_to_return' => 'parent',
'return' => 'post_id'
]
);

if ( ! $rows )
return $scanned_tag;

foreach ( $rows as $row ) {

$datums = get_post_meta( $row, "wpcf-datum", false );

foreach ( $datums as $datum ) {

setlocale(LC_TIME, 'nl_NL');

$datum = strftime("%d %B %Y", $datum);

$scanned_tag['raw_values'][] = $datum . '|' . $datum;

}
}

$pipes = new WPCF7_Pipes($scanned_tag['raw_values']);

$scanned_tag['values'] = $pipes->collect_befores();
$scanned_tag['pipes'] = $pipes;

return $scanned_tag;
}

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
hidden link

What is the link to your site?

hidden link (not the first select field, but the one at the bottom of the form) but the error is on the backend

#1911537

Hello,

I assume you don't need that custom filter in backend, you can use is_admin() to check if it is in WP backend side, if not, then add your custom filter, for example:

if(!is_admin()){
	add_filter(  ...  );
}

It should avoid the problem you mentioned above.

More help:
https://developer.wordpress.org/reference/functions/is_admin/

#1911927

That's a smart workaround, I didn't think of that. Thank you!

#1911983

I turns out not to be a solution. There is a similar error on the front-end.

I have discovered the problem: the value I get from "get_the_ID();" is sometimes not the page-ID. Apparently I am calling it inside a loop.

What would be the best way to populate toolset_get_related_posts with the ID of the page where I am displaying the form?

#1912313

My issue is resolved now.

I found the solution here:
https://toolset.com/forums/topic/problem-with-the-latest-plugin-updates/

The more global workaround is to add "if (!empty ($id))"

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