Skip Navigation

[Resolved] Filter posts with two parents

This support ticket is created 5 years, 1 month 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 6 replies, has 2 voices.

Last updated by umbertoZ 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1379277

Hi, I've a CPT that has 2 one-to-many relationships with other 2 CPTs.

I need to build a view that filter the first CPT by the two parents.

I found this solution, but I think it's based on the old Toolset relationships:

https://toolset.com/forums/topic/view-list-of-children-based-on-two-or-more-parents/

can you help me?

thanks

#1379673

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you mean that you only want to display the filtered results without offering the GUI (frontend) filters with view?

To have more accurate details on how you setup the post-relationship between your post types. Can you please share the details of your post types and how its connected? Maybe Screenshot of Toolset => Relationships page would help.

So, do you mean that You have for instance post type A and post type B both are set as parent of Post type C. So, you want to display the posts from post type C where both post type A and post type B connected?

#1379687
gui.PNG

Hi, this is my setup:

CPT Busses
CPT Trajects
CPT Timeslots

Relationships:

Busses-Timeslots one-to-many
Trajects-Timeslots one-to-many

Now I want to build a view to display a certain Timeslot depending on the Bus and Traject ID I pass by attributes.

On the GUI I can only select one related CPT (I add an image).

cheers

#1379757

Minesh
Supporter

Languages: English (English )

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

Ok - I understand what you are looking for.

To add multiple parent post filter using view's shortcode attribute, you need to use the view's filter query hook: wpv_filter-query

So, for instace, you are passing both parent Ids as view's shortcode attribute as given under:

[wpv-view name="multiple-parents-filter-view" parent1="35" parent2="40"]

-- Please note that, you do not need to add any query filter from View's Query filter section. If you have added any, please remove it.

Add the following code to your current theme's functions.php file or "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

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 == 99999 ) {
		 
	    $parent1 = $WP_Views->view_shortcode_attributes[0]['parent1'];
             $parent2 = $WP_Views->view_shortcode_attributes[0]['parent2'];
		

$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' => 'busses-timeslots'  /// change the relationship slug if required
            ),
            array(
                'role' => 'child',
                'related_to' => $parent2 ,
                'relationship' => 'trajects-timeslots'    /// change the relationship slug if required
            )
        ),
        
        'order' => 'ASC',
    )
);
$related_ids = $custom_query->posts;
 
          // passing found related ids
         $query['post__in'] = $related_ids;
  }
   
 return $query;
}

Where:
- Please replace 99999 with your original view ID.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#using-the-new-post-relationships

#1379789

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?

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

https://toolset.com/forums/topic/split-filter-posts-with-two-parents-many-to-many-relationship/

#1379795

Minesh
Supporter

Languages: English (English )

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

That's another question and different implementation as you have many to many relationships. As your original question is resolved, please mark resolve this ticket and I'll split this ticket with your many-to-many question.

#1379805

My issue is resolved now. Thank you!