Skip Navigation

[Resolved] How to display only posts who have related post

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

Last updated by Jennifer 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2412447
customer-with-projekts.jpg

Tell us what you are trying to do?

I have the following post types:

- customers (kunden) -> slug: kunden
- projects -> slug: projekt

The Post Types are in a one to many relationship. One customer can have several projects.

I have created a view (id: 1982 ) that shows all customers and in this view
I have another view (id: 1860) that shows all projects
=> with the Query Filter: Select posts in a Kunden Projekte relationship that are a related to the current post in the loop.

At the moment I can see all customers, regardless of whether a project is assigned to them or not (see picture)

I would now like to create a view that only shows customers to whom a project is assigned. How can I achieve this?

I've tried the following code, but I don't get any results.


add_filter('wpv_filter_query', 'func_filter_post_that_are_related', 10, 3);
function func_filter_post_that_are_related($query, $view_settings, $view_id) {
  global $post;
  $views = array( 1982 ); // only filter these views
  if( in_array( $view_id, $views ) ) {
    $include_ids = array(); // push all related project IDs here
    $projekt_args = array(
      'post_type' => 'projekt',
      'posts_per_page' => -1,
    );
    $projekte = new WP_Query($projekt_args); // get all clients
    foreach ($projekte->posts as $projekt ) {
       $kunden = toolset_get_related_posts(
        $projekt->ID,
        'kunde-projekt',
        'parent',
        1000000,
        0,
        null,
        'post_id',
        'child'
      );
        
        
      if( ! empty($projekt) ){
  $include_ids[] = $projekt->ID; // push the related project IDs into this array
}
       
    } 
    $query['post__in'] = isset( $query['post__in'] ) ? $query['post__in'] : array();
    $query['post__in'] = array_merge($query['post__in'], $include_ids ); // update the main query to exclude related project ids
  }
  return $query;
}

I'm grateful for any help 😉

Is there any documentation that you are following?
https://toolset.com/forums/topic/views-within-a-view/#post-1784287

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2412915

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

What if you try to replace the following line of code within the code snippet you shared:

$kunden = toolset_get_related_posts(
        $projekt->ID,
        'kunde-projekt',
        'child',
        1000000,
        0,
        null,
        'post_id',
        'parent'
      );

As the $projekt->ID you pass is the child so we need to query by role set to child and return the parent IDs.

Could you please replace above line of code within the code snippet you shared and let me know if you will require further assistance.

#2413015
no-items-found.jpg

Hello Minesh,

Thanks for your idea, but when I take the code like this, it says "No items found" on the page (see picture)


add_filter('wpv_filter_query', 'func_filter_post_that_are_related', 10, 3);
function func_filter_post_that_are_related($query, $view_settings, $view_id) {
  global $post;
  $views = array( 1982 ); // only filter these views
  if( in_array( $view_id, $views ) ) {
    $include_ids = array(); // push all related project IDs here
    $projekt_args = array(
      'post_type' => 'projekt',
      'posts_per_page' => -1,
    );
    
    $projekte = new WP_Query($projekt_args); // get all clients
    foreach ($projekte->posts as $projekt ) {
       $kunden = toolset_get_related_posts(
        $projekt->ID,
        'kunde-projekt',
        'child',
        1000000,
        0,
        null,
        'post_id',
        'parent'
      );
    
      if( ! empty($projekt) ){
  $include_ids[] = $projekt->ID; // push the related project IDs into this array
}
        
    } 
    $query['post__in'] = isset( $query['post__in'] ) ? $query['post__in'] : array();
    $query['post__in'] = array_merge($query['post__in'], $include_ids ); // update the main query to exclude related project ids
  }
  return $query;
}

Do you have any other idea how it could work. Thank you for your help.
Jenny

#2413039

Minesh
Supporter

Languages: English (English )

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

Could you please share problem URL and admin access details and let me see whats going wrong with your setup. Please also let me know that where you added the filter "wpv_filter_query" code you shared.

#2413653

Minesh
Supporter

Languages: English (English )

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

I've changed the method as you are using one-to-many post relationship we have another workaround.

As you can see with the parent view, we are passing the parent title to child view using the shortcode attribute: parent_title
=> hidden link

<wpv-loop>
          [wpv-view name="projekte"  parent_title='[wpv-post-title]' orderby="titel"]
</wpv-loop>

And within your child view that displays the project, we are catching the attribute we passed "parent_title" that holds the parent title using the view's shortcode: [wpv-attribute name='parent_title'] and we are adding this after the [wpv-items-found] that means it will display the parent title only when child view (projects) are found:
=> hidden link

[wpv-items-found]
	  <h2><b>[wpv-attribute name='parent_title'] </b></h2>
	<!-- wpv-loop-start -->
		<wpv-loop>

Can you please confirm it works as expected: hidden link

Note: I removed the <br /> tags you added as it will cause the issue, if you want space between any elements please use CSS instead of BR tags.

#2414109

Yes, it works !!!
My issue is resolved now. Thank you!