Skip Navigation

[Resolved] filter view query to display only children of posts where author is current user

This support ticket is created 5 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by erinJ 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1388541

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

#1388613

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?

#1388623

I solved this by inserting a downloads view into a customer's view. No custom coding was necessary.