Skip Navigation

[Resolved] Display CPT bassed on relationship in backend

This support ticket is created 3 years, 4 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 3 replies, has 2 voices.

Last updated by jesusM-6 3 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2118809
soporte toolset 07.jpg

Hello, I have the following toolset structure:

-Users "amigo" can create a lof of posts "recomendado"
-User "comercial" can create a post "comercial"
- Relationship one to many between CPT comercial - CPT recomendado

When I am logged in as "comercial" I can see all CPT "recomendado" . But I want to display only the CPT "recomendado" related with me (the logged in user).

#2118841

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand - on the admin post listing screen of post type "recomendado" on backend (admin), you would like to display only posts that is connected to "comercial" post type that is created by current loggedin user. If this is correct: there is no such feature to filter related posts in backend.

But a workaround could be to use pre_get_posts filter to filter the posts but that needs some custom programming. If you can setup few connected posts and share admin access details as well as "comercial" user access details and share what posts you expect to see when loggedin as comercial user I would be happy to check if I can share any workaround.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2120053

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

function filter_parent_post_by_author_and_display_related_post_only($query) {
  
    global $pagenow, $typenow, $current_user;
    
    if (  is_admin() and 'recomendado' === $typenow && 'edit.php' === $pagenow and in_array('comercial',$current_user->roles) ) {
         
     global $wpdb;
      $where = get_posts_by_author_sql('comercial',true,$current_user->ID);
      $res = $wpdb->get_col( "SELECT p.id FROM {$wpdb->posts} p $where");
      
      $related_post_ids = array(0);
      if(!empty($res)) {
      $related_post_ids = toolset_get_related_posts(
                                    $res[0],
                                    'comercial-expediente',
                                     'parent',
                                     100,
                                      0,
                                      array(),
                                     'post_id',
                                     'child'
                                     );
      }
      
      $query->set("post__in",$related_post_ids); 
    }
}
add_action( 'pre_get_posts', 'filter_parent_post_by_author_and_display_related_post_only');

When I logged in as "comercial" user you shared, I can see the expected posts now, Can you please confirm it works at your end as well.
- hidden link

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

#2120073

It's great! This is just what I need. Thank you!