Skip Navigation

[Resolved] Query custom view, show only parents that have published child

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

Last updated by ricardoL-4 4 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1684235

Tell us what you are trying to do?
Hi,
I created a parent CPT with a one-to-one child.
Now, I'm trying to create a view list calling only the parents that have child posts published, and skip all the draft children's.

I feel this is something it can be reached with a custom filter but I'm a bit out of my knowledge and I need your help on this, please.
Here is the code I have to call only parents with children but is not checking the published ones, I hope it helps:

add_filter('wpv_filter_query', 'jobs_with_bewerbungs_func', 101, 3);
function jobs_with_bewerbungs_func($query, $view_settings, $view_id) {
$views = array( 934 );
if ( in_array( $view_id, $views ) ) {
$ids = array();
$parents_args = array(
'post_type' => 'massagista',
'numberposts' => -1,
);
$parents = get_posts( $parents_args );
foreach($parents as $parent) {
$children = toolset_get_related_posts(
$parent->ID,
'massagista-disponivel',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
if( !is_array($children) || count($children) < 1 ) {
array_push( $ids, $parent->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;
}

#1684659

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand you want to display only parent posts that have child posts published - correct? If yes:

Can you please share problem URL where you want to display the parent posts and access details so I can review your current setup and guide you in the right direction.

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

#1684795

Minesh
Supporter

Languages: English (English )

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

It seems you forget to share the problem URL and what view you created that displays the parent posts?

#1684801

Sorry my bad,

this is the page under "disponivel agora!" view loop:
hidden link

and the view loop is:
ID 934 - Lista de Disponiveis
slug - lista-de-disponiveis

#1684823
Screenshot 2020-07-02 at 10.30.28.png
Screenshot 2020-07-02 at 10.28.19.png

To help you out with it, I have the code called "disponiveis_code" I was using on my settings, custom code so you can check.
I deactivated this code, so now is showing all without custom code filters.

I hope it makes sense 🙂

thanks!

#1684853

Minesh
Supporter

Languages: English (English )

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

Ok - so I review your current setup and I found that the view you are using ID 934 - Lista de Disponiveis - is set to display the post type "Massagistas".

I checked that you have one-to-many post relationship between: Centros Massagistas

Now, for instance this is the first item displayed under section "disponivel agora!" namely "Athena":
=> hidden link
- When I checked the "Athena" post in admin it does not have any related "Centro" post assigned and "Centro" is the parent post type here.

So, do you mean that if the "Massagistas" post does not have parent assigned - then we should skip that post and do not display that post on frontend - correct?

#1684863

No, it happens that I haven't created all the related children to all parents. I just created a few, as you can see in CPT Disponiveis.
I only did a few to see if I could call parents that have child posts published, so I can after create more and leave in a draft.

Does it make sense?

#1684893

Minesh
Supporter

Languages: English (English )

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

So, Can you please share a test example post link that should be displayed within that section?

The reason I confused because your view is set to display the child post type (Massagistas) not the parent post type (Centros). Why you are display child post type as actually you want to display the parent post - correct?

Can you please clarify this, once I understand your actual need it will be easy for me to guide you in the right direction.

#1684923
Screenshot 2020-07-02 at 11.14.36.png

Ok, I think you got confused since I have tow relations Centros and Disponiveis... let me explain it:

if you see on the picture every CPT Massagista has: Centro -> Massagista relationship and Massagista -> Disponivel relationship.
OK?

So what I want is a view that I can show Massagistas with a custom filter:
Only when a related Disponivel post has post status "published", the related Massagista post should show.
Make sense now?

#1684959

Minesh
Supporter

Languages: English (English )

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

That is the different relationship from what you have shared with your initial post here:
=> https://toolset.com/forums/topic/query-custom-view-show-only-parents-that-have-published-child/#post-1684235

However:
Can you please share a link of one example Massagistas post where I can see the related Disponivel post is assigned and that should be displayed with the view.

#1685025
explain.jpg

Ok, this is the Massagistas post where you can see the related Disponivel post is assigned
hidden link

Did a picture showing my request 🙂

#1685059

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link
- Under section "Disponível agora!" I can see only one post. Is this your expected results? If yes:

Then, I've adjusted and activated the following code to "Custom Code" section you added as given under:

add_filter('wpv_filter_query', 'jobs_with_bewerbungs_func', 101, 3);
function jobs_with_bewerbungs_func($query, $view_settings, $view_id) {
  $views = array( 934 );
  // you should not edit anything below this line
  if ( in_array( $view_id, $views ) ) {
    $ids = array();
    $parents_args = array(
      'post_type' => 'massagista',
      'numberposts' => -1,
    );
    $parents = get_posts( $parents_args );
    foreach($parents as $parent) {
      $children = toolset_get_related_posts(
        $parent->ID,
        'massagista-disponivel',
        'parent',
        1000000,
        0,
        array('post_status'=>"publish"),
        'post_id',
        'child'
      );
    
      if( !is_array($children) || count($children) < 1 ) {
        array_push( $ids, $parent->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;
}
#1685083

Excelente Minesh! that's exactly what I wanted! Sorry if I couldn't explain it well from the beginning.

#1685085

My issue is resolved now. Thank you!