Skip Navigation

[Resolved] Table with child posts

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display a View of Post Type A and include a nested View of Post Type B. If no related Post Type B exist, do not show the Post Type A post.

Solution: Create the nested Views and apply a post relationship Query Filter to the View of post type B. Set it to be related to the current post in the loop. Apply the following custom code to filter out Post Type A where there are no related Post Type B:

add_filter('wpv_filter_query', 'jobs_with_bewerbungs_func', 101, 3);
function jobs_with_bewerbungs_func($query, $view_settings, $view_id) {
  $views = array( 870 );
  $relationship_slug = 'job-bewerbung-multis';
  if ( in_array( $view_id, $views ) ) {
    $ids = array();
    $jobs_args = array(
      'post_type' => 'job',
      'numberposts' => -1,
    );
    $jobs = get_posts( $jobs_args );
    foreach($jobs as $job) {
      $bewerbungs = toolset_get_related_posts(
        $job->ID,
        $relationship_slug,
        'parent',
        1000000,
        0,
        array(),
        'post_id',
        'child'
      );
      if( !is_array($bewerbungs) || count($bewerbungs) < 1 ) {
        array_push( $ids, $job->ID );
      }
    }
    $query['post__not_in'] = isset($query['post__not_in']) ? $query['post__not_in'] : array();
    $query['post__not_in'] = array_merge($query['post__not_in'], $ids );
  }
  return $query;
}

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

This support ticket is created 5 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 17 replies, has 2 voices.

Last updated by SteffenM1628 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1163953
table sort.png

hey christian,

puh i hope you didn´t have had to much work to get this code running. works totaly fine!
Thank you soooooooooo much.

I found maybe a bug or so in the nested views.
so my child view is a sortable table. but when i display the parent view and want to sort the table it doesn´t work.
You can see it under the Dashboard menu item and than choose "Bewerbungen" in the tabs.

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

https://toolset.com/forums/topic/sorting-nested-tables/

#1164043

Okay, let's discuss the sorting table problem in the new ticket I just created: https://toolset.com/forums/topic/sorting-nested-tables/

#1164087

My issue is resolved now. Thank you!