Skip Navigation

[Resolved] Split: Filter posts with two parents – many to many relationship

This support ticket is created 4 years, 5 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.

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.

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 12 replies, has 2 voices.

Last updated by umbertoZ 4 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1379797

Thnaks for the code.

I modified it in this way:

add_filter( 'wpv_filter_query', 'func_filter_with_multiple_parents', 10, 3 );
function func_filter_with_multiple_parents( $query, $view_settings, $view_id ) {
  global $WP_Views;

  if( $view_id == 380 ) {

    $parent1 = $WP_Views->view_shortcode_attributes[0]['busid'];
    $parent2 = $WP_Views->view_shortcode_attributes[0]['trajectid'];


    $custom_query = new WP_Query( 
      array(
        'post_type' => 'timeslot',   /// change the post_type slug if required 
        'fields'=> 'ids',
        'posts_per_page' => -1,
        'toolset_relationships' => array(
          array(
            'role' => 'child',
            'related_to' =>$parent1,
            'relationship' => 'bus-timeslot'  /// change the relationship slug if required
          ),
          array(
            'role' => 'child',
            'related_to' => $parent2 ,
            'relationship' => 'traject-timeslot'    /// change the relationship slug if required
          )
        ),

        'order' => 'ASC',
      )
    );
    $related_ids = $custom_query->posts;

    // passing found related ids
    $query['post__in'] = $related_ids;
  }

  return $query;
}

I tested it with some ids and it works:

[wpv-view name="timeslots-test" trajectid="191" busid="317"]

But it doesn't work where I need.

I've also a many-to-many relationship btw trajects and busses. I built a view to display a list of intermediary posts of this relationship. There I want to include the other view in this way:

[wpv-view name="timeslots-test" trajectid="[wpv-post-id item='@traject-bus.parent']" busid="[wpv-post-id item='@traject-bus.child']"]

This gives me:

"The site is experiencing technical difficulties."

I've also made a test including just this in the view:

trajectid="[wpv-post-id item='@traject-bus.parent']" busid="[wpv-post-id item='@traject-bus.child']"

And it works fine, the output is correct, a list of parent and child ids of the intermediary CPT.

any idea?

#1379803

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As you shared the details, you have many-to-many relationship btw trajects and busses. And you set the "timeslots-test" view to query intermediary posts of this relationship that means this view will display the intermediary posts - correct?

But you want to filter this "timeslots-test" view that is set to query intermediary post type, you want to pass both trajects and busses ID. But I would like to know first where you will add the "timeslots-test" view? on separate page or where exactly?

Can you please share problem URL where you added the "timeslots-test" view and access details?

*** 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.

#1379853

Minesh
Supporter

Languages: English (English )

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

There is something really strange things happening.

I try to load the test page you created: hidden link

But even I changed some text in backend editor it does not reflect on the frontend.: hidden link

Then when I checked the view source of the page: view-source:hidden link

I see page stuck at the entry content. There must be some error/fatal error on that page. Can you please activate WP_DEBUG from your wp-config.php file and checked if you see any errors?

If you do not see anything, please activate the PHP debugging:

More info:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#php-debugging

#1380059

Hi, DEBUG is on.

I've also added function log_me($message)

cheers

#1380219

Minesh
Supporter

Languages: English (English )

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

Ok - there was a fatal error. Test page works smoothly here: hidden link

I also see now debug is off. I can not see the error now. I need FTP access details for that.

I've removed another view that was added to that. Now, with the following view - I see you set this view to query by intermediary post type of many-to-many relationship.
=> hidden link

I've added this view to separate page now - I see debug is not active and page is stuck at the middle. mustbe the fatal error but without activating the debug, I can not see anything.
=> hidden link

I checked further and I see you have added another view to loop editor section of the above view. Can you please tell me what exact results you want to display with this view?

[wpv-view name='test-timeslots' trajectid='[wpv-post-id item="@traject-bus.parent"]' busid='[wpv-post-id item="@traject-bus.child"]']<br><br>

As when you query intermediary post type you will be able to display both related parent and child item. Can you please share your expected output with many-to-many relationship. what exactly you want to display?

#1380327

Hey, I noticed that now I cannot edit Toolset>Settings>Custom code, did you block it?

cheers

#1380743

Minesh
Supporter

Languages: English (English )

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

I do not made any changes or I do not have such permissions. Even I do not able to access the custom code section, for me as well its blocked.

Can you please send grant permissions so that I can at least access the custom code section.

With the private key you sent to me, do I need to generate the .pem file using the puttygen and then I should use the pem file for login? if yes, what should be the user I should use to login as it asks me to login as what user?

#1380845

Hey, the custom code issue is fixed.

I added SFPT login details under "FTP details" on the private message.

cheers

#1380887

Minesh
Supporter

Languages: English (English )

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

It was really a highly complicated requirement and again it needs to be fulfilled by view's hook.

I've added/modified the code as given under to custom code section.


add_filter( 'wpv_filter_query', 'func_filter_with_multiple_parents', 10, 3 );
function func_filter_with_multiple_parents( $query, $view_settings, $view_id ) {
  global $WP_Views;

  if( $view_id == 380 ) {

    $parent1 = $WP_Views->view_shortcode_attributes[0]['busid'];
    $parent2 = $WP_Views->view_shortcode_attributes[0]['trajectid'];


    $custom_query = new WP_Query( 
      array(
        'post_type' => 'timeslot',   /// change the post_type slug if required 
        'fields'=> 'ids',
        'posts_per_page' => -1,
        'toolset_relationships' => array(
          array(
            'role' => 'child',
            'related_to' =>$parent1,
            'relationship' => 'bus-timeslot'  /// change the relationship slug if required
          ),
          array(
            'role' => 'child',
            'related_to' => $parent2 ,
            'relationship' => 'traject-timeslot'    /// change the relationship slug if required
          )
        ),

        'order' => 'ASC',
      )
    );
    $related_ids = $custom_query->posts;

    // passing found related ids
    $query['post__in'] = $related_ids;
  }
  
  if( $view_id == 395 ) {

   
    $parent2 = $WP_Views->view_shortcode_attributes[1]['trajectid'];
    $parent1 = $WP_Views->view_shortcode_attributes[1]['busid'];
 
    
  $custom_query = new WP_Query( 
      array(
        'post_type' => 'timeslot',   /// change the post_type slug if required 
        'fields'=> 'ids',
        'posts_per_page' => -1,
        'toolset_relationships' => array(
          array(
            'role' => 'child',
            'related_to' =>$parent1,
            'relationship' => 'bus-timeslot'  /// change the relationship slug if required
          ),
          array(
            'role' => 'child',
            'related_to' => $parent2 ,
            'relationship' => 'traject-timeslot'    /// change the relationship slug if required
          )
        ),

        'order' => 'ASC',
      )
    );
    $related_ids = $custom_query->posts;
     
    
    if(!empty($related_ids)) {
       // passing found related ids
       $query['post__in'] = $related_ids;
    }

   
  }

  return $query;
}

This is the page where you can see the results:
=> hidden link

I've created the following views:
=> hidden link
- Above view is set to query the intermediary post type

=> hidden link
- Above view is set to query the timeslot post type.

#1381237

It looks great!! Later I'll test it.

Can you split this ticket, I've another issue with Validation, but I'd like you go on with it as you already knows the wp-admin setup if needed.

I've add and edit Timeslots forms. Both have the Bus parent field to connect the timeslot with a Bus.

What I need is to validete it to be sure it is not empty.

I tried this code, but it doesn't work for me:

add_filter('cred_form_validate','my_form_validate',10,2);
function my_form_validate ($field_data, $form_data) {

  list($fields,$errors)=$field_data;

  if ($form_data['id']==217) {
    
    $bus = $_POST['@bus-timeslot_parent'];
    
    if (empty($bus))
    {
      log_me('Empty Bus');
      $errors['@bus-timeslot_parent'] = 'You have to select a Bus';
    } else {
      log_me('NO Empty Bus');
    }

  }
  return array($fields,$errors); 
 
}

What I would like to achive is that the user cannot submit the form if the parent field is not set. Another complications is that I have many forms on the same page.

This is the page:

/dispatch/admin-dashboard/

This is the form:

/wp-admin/post.php?post=217&action=edit&lang=en

This is the view that display forms:

/wp-admin/admin.php?page=views-editor&view_id=151

thanks for the assistance.

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-split-filter-posts-with-two-parents-forms-api-help-required/

#1381631

Minesh
Supporter

Languages: English (English )

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

The issue was really hard but I'm glad to know that the solution I shared helps you to resolve your issue.

As per your request, I'll split the ticket and feel free to mark resolve this ticket.

#1381855

Minesh
Supporter

Languages: English (English )

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

Please feel free to mark resolve this ticket.

#1381929

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.