Tell us what you are trying to do?
I have a one-to-many post-relationship between two custom post types, "companies" (parent) and "downloads"(child).
I have a custom user role, "customer".
Each customer can publish one company, only admins can publish downloads.
I want to modify a views query so that a customer can only view downloads related to the company that they are the author of.
here is the code that I'm using:
function get_user_company(){
$current_user = get_current_user_id();
$company_query_args = array(
'post_type' => 'client_company',
'post_author' => $current_user,
'post_status' => 'publish',
'posts_per_page' => 1,
'return' => 'ids'
);
$company_query = new WP_query( $company_query_args );
$company = '';
while ( $company_query->have_posts() ) : $company_query->the_post();
$company .= get_the_ID() ;
endwhile;
return $company;
}
function downloads_views_query( $query_args, $view_settings, $view_id ) {
$user_company = get_user_company();
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 2470) ) {
$query_args['toolset_relationships'] = array(
'role' => 'child',
'related_to' => $user_company,
'relationship' => 'company-download'
);
}
return $query_args;
}
However, the query isn't returning any results. Is there another way to achieve this? I haven't been able to find documentation to achieve exactly what I'm looking for.
Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/
https://toolset.com/forums/topic/access-to-the-loop-in-a-view/
Is there a similar example that we can see?
What is the link to your site?
hidden link
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
First of all, I would like to know where you are going to display the downloads that are related to the company? is it on the company page for which the current user is an author?
I solved this by inserting a downloads view into a customer's view. No custom coding was necessary.