Skip Navigation

[Resolved] Can you toolset_get_related_post(s) function to retrieve posts by user ID?

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

Problem:
The user would like to programmatically get related posts that are created by the current user.

Solution:
That's not possible with the Toolset API. We need to, first, get the related posts, then we can use a wp_query to get the current user's posts within them. This is an example code:

$applications = toolset_get_related_posts($post->ID, 'job-to-job-application', 'child');
$posts = get_posts(array(
    'author' => get_current_user_id(),
    'post__in' => $applications
));
if( count( $posts ) > 0) {
    echo 'View Application';
}
else {
    echo 'Apply';
}

Relevant Documentation:

This support ticket is created 2 years, 7 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by himanshuS 2 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2165919

I have two post types - Jobs and Job Applications. The two are in an O2M relationship.

A user should be able to submit only one application for a job post. Therefore, I want to dynamically change the CTA on the Job post for logged-in users based on 1) have they submitted an application 2) have they not submitted an application.

I just want to check if the current user has a child post for a given job post. It seems like I am not able to filter based on the current user ID in toolset_get_related_post function. Can you suggest the best way to check if the post exists or not? I don't want to loop over every single job application and then check for the user id to find the output.

This is the code I am using to run on the Job post template.

<?php global $post;

$current_user_id = get_current_user_id();

$arg = array('author' => $current_user_id);

$application_post_id = toolset_get_related_post($post->ID, 'job-to-job-application', 'child', $arg);

if($application_post_id != 0) {
	echo 'View Application';
}
else {
	echo 'Apply';
}
	
?>
#2166559

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting Toolset support.

The toolset_get_related_post and toolset_get_related_posts functions do not accept an argument for the author. So, you will need to use another query to get the user's posts. Something like:

$applications = toolset_get_related_posts($post->ID, 'job-to-job-application', 'child');
$posts = get_posts(array(
    'author' => get_current_user_id(),
    'post__in' => $applications
));
if( count( $posts ) > 0) {
    echo 'View Application';
}
else {
    echo 'Apply';
}

- https://developer.wordpress.org/reference/classes/wp_query/#author-parameters
- https://developer.wordpress.org/reference/functions/get_posts/

I hope this helps. Let me know if you have any questions.

#2166921

My issue is resolved now. Thank you!

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