Skip Navigation

[Resolved] View Empty Relationships

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

Last updated by Minesh 9 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2702768

Is there a way to use the Query Filter in views (legacy) to only display posts that have an empty relationship ?
I can use a condition in the views loop that works (with page breaks) but it lists all 30,000 posts.
I just want it to show only the few posts that have no relationship.

#2702799

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share details about what post relationship do you have and between what post types and what is the post relationship slug and for what post type you created the view.

#2702807
Screenshot-2.jpg
Screenshot-1-4.jpg
Screenshot-1-3.jpg
Screenshot-1-2.jpg
Screenshot-1.jpg

It's a one to many relationship (nation-sc_ladies-results) and the view is in ladies-results.

The conditional setting in the attached loop works but I would like to have it work at the Query filter.
The query filter setting as shown does not work .

#2702839

Minesh
Supporter

Languages: English (English )

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

ok - You can use the Toolset View's hook "wpv_filter_query" in order to filter the view and pass the query arguments on fly.

For example:
You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_filter('wpv_filter_query', 'func_show_only_child_where_parent_is_set', 10, 3);
function func_show_only_child_where_parent_is_set($query, $view_settings, $view_id) {
  
   $allowed_views = array( 999999);

  
  if ( in_array( $view_id, $allowed_views ) ) {

    $ids = array();

    /// replace parent post type slug and relationship slug if required
    $patent_post_type_slug = 'nation';
    $relationship_slug = 'nation-sc_ladies-results';
   
    $parents_args = array(
      'post_type' => $patent_post_type_slug,
      'numberposts' => -1,
    );
    $parents = get_posts( $parents_args );
    
    foreach($parents as $parent) {
      $children = toolset_get_related_posts(
        $parent->ID,
        $relationship_slug,
        'parent',
        1000000,
        0,
        array('post_status'=>"publish"),
        'post_id',
        'child'
      );
     
      if( is_array($children) and count($children) >= 1 ) {
        array_push( $ids,$children);
      }
    
    }
    $query['post__in'] = isset($query['post__in']) ? $query['post__in'] : array();
    $query['post__in'] = array_merge($query['post__in'], $ids );
  }
  return $query;
}

Where:
- Replace 999999 with your original view ID.

More info
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Please let me know how it goes.

#2702872

I made two adjustments
- 999999 to 121817
- nation to nation-sc

Now no results are found when there should be a few results found.

#2702987

Minesh
Supporter

Languages: English (English )

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

Can you please share where on what page you have added the view and what view you are using and send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2703168

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've adjusted code added to "Custom Code" section as given under:

add_filter('wpv_filter_query', 'func_show_only_child_where_parent_is_set', 10, 3);
function func_show_only_child_where_parent_is_set($query, $view_settings, $view_id) {
   
   $allowed_views = array( 121817);
  
  if ( in_array( $view_id, $allowed_views ) ) {
 
    $ids = array();
 
    /// replace parent post type slug and relationship slug if required
    $patent_post_type_slug = 'nation-sc';
    $relationship_slug = 'nation-sc_ladies-results';
   
    
    $parents_args = array(
      'post_type' => $patent_post_type_slug,
      'numberposts' => -1,
    );
    $parents = get_posts( $parents_args );
        
    foreach($parents as $parent) {
      $children = toolset_get_related_posts(
        $parent->ID,
        $relationship_slug,
        'parent',
        1000000,
        0,
        array('post_status'=>"publish"),
        'post_id',
        'child'
      );
      
     if( is_array($children) and count($children) >= 1 ) {
        array_push( $ids,$children);
     }
      
    }
    
    $query['post__not_in'] = isset($query['post__not_in']) ? $query['post__not_in'] : array();
    $query['post__not_in'] = array_merge($query['post__not_in'],  array_unique($ids) );
    
  }
  return $query;
}

Can you please confirm it works as expected now.

#2703174

It did work when you used a migrated code but then you removed it.
Now it just lists every post, I just want the posts missing the relationship.

#2703193

Minesh
Supporter

Languages: English (English )

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

There must be cache issue going on. I've adjusted the code again to migrated relationship as given under:

add_filter('wpv_filter_query', 'func_show_only_child_where_parent_is_set', 10, 3);
function func_show_only_child_where_parent_is_set($query, $view_settings, $view_id) {
   
   $allowed_views = array( 121817);
 
   
  if ( in_array( $view_id, $allowed_views ) ) {
 
    $ids = array();
 
    /// replace parent post type slug and relationship slug if required
    $patent_post_type_slug = 'nation-sc';
   // $relationship_slug = 'nation-sc_ladies-results';
    $migrated_relationship_slug = array('nation-sc','ladies-results');
    
    
    $parents_args = array(
      'post_type' => $patent_post_type_slug,
      'numberposts' => -1,
    );
    
    $parents = get_posts( $parents_args );
        
    foreach($parents as $parent):
      $children = toolset_get_related_posts(
        $parent->ID,
        $migrated_relationship_slug,
        'parent',
        1000000,
        0,
        array('post_status'=>"publish"),
        'post_id',
        'child'
      );
      
     if( is_array($children) and count($children) >= 1 ) {
        $ids = array_merge( $ids,$children);
     }
      
    endforeach;
   
    
    $query['post__not_in'] = isset($query['post__not_in']) ? $query['post__not_in'] : array();
    $query['post__not_in'] = array_merge($query['post__not_in'],  array_unique($ids) );
    
  }
  return $query;
}

Can you please confirm it works as expected now.

#2703212

Not sure what to say. Yes cache was an issue but so was the 3 or 4 people you had looking at it.

#2703220

Minesh
Supporter

Languages: English (English )

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

I'm not sure who else is looking at this ticket as I'm the one who is actively working on this ticket 🙂

Maybe I tried proxy to access the website that is why you might be thinking that 3-4 people accessing the website but I'm not sure who else other is looking at your site 🙂

Glad to share the solution and you are welcome to mark resolve this ticket.

#2703225

Thanks working fine now.

#2703363

I tried to use the same coding with alterations for different relationships but had no success.

The ladies-results post type has 4 different one to many relationships. I'm trying to make 4 different views that will find the posts that are missing any of these relationships. It works for the nation-sc to ladies-results relationship but not for the others. The other 3 are ladies, ladies-event, and club-sc.
When I change the slugs and view id, it either never loads the page, or crashes. Even if I lower the amount of posts to display to 2.

You can use the same view id just swap out different custom code for each. As I noticed that a fatal error occurred if I had 2 codes active.
Thanks

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

https://toolset.com/forums/topic/split-view-empty-relationships-check-for-other-post-relationship/

#2703413

Minesh
Supporter

Languages: English (English )

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

As the original question is already address with this support ticket.

I urge you to open a new ticket with every new question you may have. This will help other users searching on the forum as well as help us to write problem resolution summery for the original issue reported with this ticket.

I'll split the ticket with this new question and I will get in touch with you with the following split ticket:
- https://toolset.com/forums/topic/split-view-empty-relationships-check-for-other-post-relationship/

You can mark resolve this ticket with different message.