Minesh,
I have tried using the API function but I get an error.
It happens because I am using this inside another query in elementor to query specific posts
This works fine on it's own
$project_submission_posts_array = toolset_get_related_posts(
// get posts related to this one
array('parent' => $assigned_project_prompt_list_array),
// Relationship between the posts
'project-prompt-to-project-submission',
// Additional arguments
[
// Get posts where company_id is the parent in given relationship.
// This is mandatory because we're passing just a single post as the first parameter.
// 'query_by_role' => 'parent',
'role_to_return' => 'child',
'return' => 'post_id',
'post_status' => 'publish',
]
);
This does not work with elementor query :
add_action( 'elementor/query/project-submission-submitted-for-current-user', function( $query ) {
$user_id = get_current_user_id();
$assigned_project_prompt_list_object = get_user_meta($user_id,''field-slug', true);
if($assigned_project_prompt_list_object[0] != null) {
$assigned_project_prompt_list_json = $assigned_project_prompt_list_object[0];
$assigned_project_prompt_list_array = json_decode($assigned_project_prompt_list_json, true);
$project_submission_posts_array = toolset_get_related_posts(
// get posts related to this one
array('parent' => $assigned_project_prompt_list_array),
// Relationship between the posts
'project-prompt-to-project-submission',
// Additional arguments
[
// Get posts where company_id is the parent in given relationship.
// This is mandatory because we're passing just a single post as the first parameter.
// 'query_by_role' => 'parent',
'role_to_return' => 'child',
'return' => 'post_id',
'post_status' => 'publish',
]
);
$query->set( 'post__in', $project_submission_posts_array);
/*
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wpcf-project-prompt',
'value' => $assigned_project_prompt_list_array,
// 'type' => 'INT',
'compare' => 'IN'
),
)
);
*/
// $query->set( 'post_status', [ 'publish' ]);
} // if project_prompt list is not null
else{
$query->set( 'post__in', [ '999999' ]);
}
} );
?>
Error thrown:
Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246 Stack trace: #0 /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, NULL) #1 /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(Array, 'project-prompt-...', Array) #2 /home/customer/www/domain/public_html/wp-content/toolset-customizations/filter-project-submission-for-employer-access.php(25): toolset_get_related_posts(Array, 'project-prompt-...', Array) #3 /home/customer/www/domain/public_html/w in /home/customer/www/domain/public_html/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246
What am I missing?
What is the slug for parent post reference field? is it stored in the child post as a field? something like wpcf--?
I just need that parent post ID but it seems like wpcf-project-prompt is not the right id.