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 ?
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'
]
);
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 !
My issue is resolved now. Thank you!