Skip Navigation

[Resolved] toolset_get_related_posts -> searching for meta value throw an error

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

Problem: I'm trying to add a meta_key query to a toolset_get_related_posts query, but a PHP error is thrown.

Solution: Verify your parent post ID variable is populated correctly using server logs or a var_dump.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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

Last updated by philippeK 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1388273

I created this short code, but it throws an error. It looks like the problem is on the array part where I search for meta_key meta_value meta_compare:

add_shortcode('wpv_nbr_child_post', 'wpv_nbr_child_post_function');
function wpv_nbr_child_post_function($atts) {
	extract( shortcode_atts( array(
        'parent_post_id' => '',
        'what_content' => '', // child cpt slug
    ), $atts ) );
		
	if($what_content == "interview"){
		$related_posts = toolset_get_related_posts(
                                    $parent_post_id,
                                    'person-1min-interview',
                                     'parent',
                                     100,
                                     0,
                                     array( 'meta_key'   => 'wpcf-type-of-content', 
												'meta_value' => 'short-interview',
										   		'meta_compare' => '='
										   		),
                                     'post_id',
                                     'child'
                                     );
		return count($related_posts);
	}elseif($what_content == "meditation"){
		$related_posts = toolset_get_related_posts(
                                    $parent_post_id,
                                    'person-1min-interview',
                                     'parent',
                                     100,
                                      0,
                                       array( 'meta_key'   => 'wpcf-type-of-content', 
												'meta_value' => 'meditation-video',
										   		'meta_compare' => '='
										   		),
                                     'post_id',
                                     'child'
                                     );
		return count($related_posts);
	}
	return 0;	
}

this is the error that I get:
Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /var/www/html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php:228 Stack trace: #0 /var/www/html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php(163): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'parent') #1 /var/www/html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/public_api/m2m.php(107): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct('', 'person-1min-int...', Array) #2 /var/www/html/wp-content/themes/innerchange/functions.php(157): toolset_get_related_posts('', 'person-1min-int...', 'parent', 100, 0, Array, 'post_id', 'child') #3 /var/www/html/wp-includes/shortcodes.php(325): wpv_nbr_child_post_function(Array, '', 'wpv_nbr_child_p...') #4 [internal function]: do_shortcode_tag(Array) #5 /var/www/html in /var/www/html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 228

What am I doing wrong ?

#1388293

Assuming your $parent_post_id and $what_content variables are populated correctly, I think the syntax could be updated. Try this alternate syntax for your toolset_get_related_posts calls and let me know the results:


$related_posts = toolset_get_related_posts(
  $parent_post_id,
  'person-1min-interview',
  [
    'query_by_role' => 'parent',
    'limit' => 100, // 100 is the default, you could delete this line
    'offset' => 0, // 0 is the default, you could delete this line
    'args' => array( 
      'meta_key'   => 'wpcf-type-of-content', 
      'meta_value' => 'short-interview',
      'meta_compare' => '='
    ),
    'return' => 'post_id', // post_id is the default, you could delete this line
    'role_to_return' => 'child'
  ]
);

...

$related_posts = toolset_get_related_posts(
  $parent_post_id,
  'person-1min-interview',
  [
    'query_by_role' => 'parent',
    'limit' => 100, // 100 is the default, you could delete this line
    'offset' => 0, // 0 is the default, you could delete this line
    'args' => array( 
      'meta_key'   => 'wpcf-type-of-content', 
      'meta_value' => 'meditation-video',
      'meta_compare' => '='
    ),
    'return' => 'post_id', // post_id is the default, you could delete this line
    'role_to_return' => 'child'
  ]
);
#1388305

it works but the error was coming from the fact that parent_post_id was not passed correctly to the short code... silly me...
Either code works, but yours make more sens.

Thanks !

#1388307

My issue is resolved now. Thank you!