Skip Navigation

[Closed] Toolset_get_related_posts does not work for a user type

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.

This topic contains 3 replies, has 2 voices.

Last updated by Christopher Amirian 1 year, 3 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2529221

I am using the following filter with elementor to identify children of a custom post called company.
It works for admin but does not work for employer user type.
Are there access controls for API for toolset_get_Related_posts()

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_action( 'elementor/query/acton-name', function( $query ) {
	// Here we set the query to fetch posts with 
	// post status 'publish' and 'draft'.
  $current_user_id = get_current_user_id();
  $company_id = get_user_meta($current_user_id, 'field-name', true);
  error_log('Inside action.');
  if( $company_id != null) {
//  $job_posts_array = array();
    error_log('Company ID is not null. It is ');
    error_log ($company_id);
  //get all jobs related to the company    
     $job_posts_array = toolset_get_related_posts( 
    // get posts related to this one
    $company_id, 
    // Relationship between the posts
    'company-to-job', 
    // 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' => 'draft,private,publish',
    ]
  );    

    error_log('searched for job posts. Answer is ');
    $array_string = implode(',', $job_posts_array);
    error_log($array_string);
   if( !empty($job_posts_array)) { 
       error_log('Job posts array is not null');
    $query->set( 'post__in', $job_posts_array);                             
	$query->set( 'post_status', [ 'publish', 'draft', 'private'] );
   } // if job posts are there
    else {
    $query->set( 'post__in', array('000000'));   
                }
    
  } // check if company id is not null
  else {
    $query->set( 'post__in', array('000000'));   
                }
} );
?>

Error log:
[07-Jan-2023 14:43:18 UTC] Inside action.
[07-Jan-2023 14:43:18 UTC] Company ID is not null. It is
[07-Jan-2023 14:43:18 UTC] Company-id
[07-Jan-2023 14:43:18 UTC] searched for job posts. Answer is
[07-Jan-2023 14:43:18 UTC]

I am really stuck here. What am I missing?

#2530149

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I am not sure what is the issue as the function in question just gets the proper ID or WP_Post and it should work ok.

I suggest that you check if you have access to the ID of the post with the other user role or not.

This answer might help you do the troubleshooting:

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

So what I think is happening is that the user role that you want to use somehow does not have access to the post in question. But I am not 100% sure.

It is just an idea to think about.

Thanks.

#2530259

Thanks for the response.

The post type in question is jobs.
The settings for the target user gives them access to private posts - hidden link

The private jobs are queried by admin but not this custom user type. Both are done with toolset_get_related_posts.

More context:
The post that is not queries is an auto-expired post. It is in private status (which I assume is true for all auto expired posts).
I used Toolset forms to set auto-expire on job posts. Are auto-expired posts treated differently? Do I need to change the arguments to capture them?

#2530359

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I am not sure about that. From what I see here:

https://toolset.com/course-lesson/setting-up-automatic-post-expiration/

You can set the status to be whatever you want for the expiration. So maybe you can test and see if you change the staus the code works or not.

That way you will see if the problem is that or not.

Thanks.

The topic ‘[Closed] Toolset_get_related_posts does not work for a user type’ is closed to new replies.