Skip Navigation

[Gelöst] Table with child posts

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 5 Jahren, 11 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 17 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von SteffenM1628 vor 5 Jahren, 11 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1160861

Tell us what you are trying to do?
I want to create a view which is a a heading and a table.
Heading: name of the parent post type
table: list all child relationships to that heading (parent post)

example:
Parent post type is jobs and child is Application. (one to many relationship)

Heading: Job A (parent)
Table: List of all Applications (child relationship to the heading)

hope this helps.
cheers

#1160900

Hi, it sounds like something you can accomplish with two Views.
- Create a View of Applications. Add a Post Relationship Query Filter. If you cannot find the Query Filter panel, scroll to the top right corner and click "Screen Options" to activate the panel. Then you will add a post relationship filter, choose the Job / Application relationship, and set it up so the parent post is the current post in the loop.
- In the Loop Editor area of the View of Applications, use the Loop Wizard to create a table structure. You can choose different information to display about each Application, and the wizard will create the table for you.
- Next, create a View of Jobs. Do not add any Query Filters.
- In the Loop Editor area of the View of Jobs, choose an unformatted loop. Insert the post title or post link. Then use the Fields and Views button to insert the View of Applications inside the wpv-loop tags.
- Place the View of Jobs on your site in a custom page, or another post or template.

Let me know if you have questions about this approach.

https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

#1161670
appblication view query.png
Loop with job view.png

Hey Christian,

i didn´t get it running.

how do i set the parent post type in the loop and where do i have to put the parent view in the loop?

some pics attached.

Cheers

#1162375
PARENT:CHILD LOOP.png

Hey Christian,

i have this working now. but few more question about filtering are coming up now.
Attached you will find a screenshot with the problems explained.

Thanks for your support.

#1162433

I need to show only those posts where the current user is the author of the post.
Add a post author Query Filter to the View of Jobs.

Hide all posts in the loop where no relation is set.
Please share the code from the loop editor of both Views so I can see how you have this set up so far.

#1162483
queries.png

i have added a filter: "author is current user logged in user" but nothing is changing in the view.

Code of the job (parent) loop with queries ("author is current user logged in user" & relationship current post in the loop)

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-jobs-parent"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

code of the application (child) loop with no queries:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<table width="100%">
		<thead>
			<tr>
				<th>[wpv-heading name="post-title"]Parent[/wpv-heading]</th>
				<th>[wpv-heading name="post-title"]Child[/wpv-heading]</th>
			</tr>
		</thead>
		<tbody class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<tr>
				[wpv-post-body view_template="loop-item-in-bewerbung-child"]
			</tr>
		</wpv-loop>
		</tbody>
	</table>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

loop item application (child):

<td>[wpv-view name="jobs-parent"]</td>
<td>[wpv-post-title]</td>

thanks

#1162495
Screen Shot 2018-12-09 at 5.04.21 PM.png

Code of the job (parent) loop with queries ("author is current user logged in user" & relationship current post in the loop)
The View of Jobs should not have a post relationship filter. The post relationship filter should be applied to the View of Applications, because it is the child post View. The View of Jobs should only have a post author filter. If you also want to filter Applications by post author, you should add a Post Author Query Filter to the View of Applications.

[wpv-post-body view_template="loop-item-in-jobs-parent"]

I'm not able to see the contents of this template, but I assume it includes the shortcode to display your View of Applications. If I am correct, this looks good.

<td>[wpv-view name="jobs-parent"]</td>
<td>[wpv-post-title]</td>

This is from the Loop template in the View of Applications. It looks like you have included a 3rd View here - "jobs-parent". If you only want to display the parent post title, you can use the wpv-post-title shortcode instead of another View. When you use the Fields and Views button to insert the Post Title, the popup includes a tab called "Post Selection" in English, "Beitragsauswahl" in German. In this tab, you can select the parent Job post. See the attached screenshot.

If the results are not what you expect, I'll need to log in and take a look in your wp-admin area. Please let me know where I can see the View on the front-end of the site.

#1162994
Screen Shot 2018-12-10 at 12.13.39 PM.png

nope, does not work.
See my comment above:
https://toolset.com/forums/topic/table-with-child-posts/#post-1160900

- Place the View of Jobs on your site in a custom page, or another post or template.

You should place the parent View in your page, not the child View. I created a test page with the parent View and added some test content using the User profile you provided. Please log in using the same User profile and go to versteckter Link to see the View working. I am attaching a screenshot here.

#1163007

Oh man... sometimes i do not see the little thinks in life 😉
Great, works perfect.

To have no "No items found" i have deleted the
[wpml-string context="wpv-views"]Not items found[/wpml-string] code

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<table width="100%">
		<thead>
			<tr>
				<th>[wpv-heading name="post-title"]Parent[/wpv-heading]</th>
				<th>[wpv-heading name="post-title"]Child[/wpv-heading]</th>
			</tr>
		</thead>
		<tbody class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<tr>
				[wpv-post-body view_template="loop-item-in-bewerbung-child"]
			</tr>
		</wpv-loop>
		</tbody>
	</table>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	[/wpv-no-items-found]
[wpv-layout-end]

Is this ok? because it does the job 😉 or do i break some other part with that?

#1163103
many.png

Hey Christian,

damn i made a mistake. I have set up a wrong relationship. i choose a one (jobs) to many (applications) but it must be a many to many relation.
But with that i can´t select the jobs in many to many. (pic attached)

Could you guide me please?

#1163149

To have no "No items found" i have deleted the
[wpml-string context="wpv-views"]Not items found[/wpml-string] code

Perfect, this is the correct way to remove the message "No items found".

But with that i can´t select the jobs in many to many. (pic attached)
Yes, because more than one Job can be related to the current Bewerbung. The proper way to include related Job information inside the View of Bewerbungs would be to use a View of related Jobs...but that doesn't really make sense for you. Instead, you should remove the Job title from the Bewerbung View and place it in the Job View.

#1163157

Yes, because more than one Job can be related to the current Bewerbung. The proper way to include related Job information inside the View of Bewerbungs would be to use a View of related Jobs...but that doesn't really make sense for you. Instead, you should remove the Job title from the Bewerbung View and place it in the Job View.

Allright, did that but the application view does not show anything. i only the the job title from the job view.

#1163167
no relation.png

hey christian,

got this going ?

just one quest left.

Now i see all my jobs with relations to applications and without.
I just want to see only the jobs which have an relation. is there a conditional i can use?

#1163511

hey christian,

found this here:
https://toolset.com/forums/topic/hide-parent-view-if-child-view-is-empty/

but the problem is again the many to many relation because i can´t select the parent like in the topic mentioned.

#1163849

Okay yes I see the problem. I have added the following custom code filter that will only return Jobs that have child Bewerbungs:

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;
}

You can find this code in Toolset > Settings > Custom Code. Please check the results on the front-end of the site and let me know if the list is not displaying as expected.