Skip Navigation

[Resolved] Only display parent posts that have children with a custom field value

This support ticket is created 3 years 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 26 replies, has 3 voices.

Last updated by Rita 3 years ago.

Assisted by: Jamal.

Author
Posts
#2006689

Hi there

I have a very nice filter in place, kindly submitted by one of the support team on another support post, to filter views results to only display posts that have children. However, I would like to see if its possible to tweak this filter to add an additional condition to only display posts that have children AND if any of those children have a certain custom field value.

In this case, we have parent 'events' and child 'tickets'.

Child 'tickets' have a custom select field with values of 1, 2, 3 or 4.

I would like the view to ONLY display the parent 'event' post if:

1. It has child 'tickets'
2. Any one of those child 'ticket' posts has a custom field value of 1.

Here is the filter I currently have in functions.php:

add_filter('wpv_filter_query', 'events_with_tickets_func', 101, 3);
function events_with_tickets_func($query, $view_settings, $view_id) {
$views = array( 1075, 15777, 1147, 167 );
$relationship_slug = 'event-ticket';
$parent_type_slug = 'event';
// you should not edit anything below this line
if ( in_array( $view_id, $views ) ) {
$ids = array();
$parents_args = array(
'post_type' => $parent_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_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;
}

Do you think it is possible?

Hoping you can help and thank you in advance!

Regards

Rita

#2007405

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As you can see with the post relationship API function: toolset_get_related_posts()
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- It supports the meta_key and meta_value arguments.

So what if you try to replace your existing toolset_get_related_posts() with the following where we mentioned the meta_key and meta_value arguments.

$children = toolset_get_related_posts(
$parent->ID,
$relationship_slug,
'parent',
1000000,
0,
array('meta_key' => 'wpcf-your-field-slug',
      'meta_compare' => '=',
      'meta_value' => '1'),
'post_id',
'child'
);

Where:
- Replace your-field-slug with your original field slug.

Does that helps?

#2007695

Hi Minesh
Yes that works beautifully. Thank you very much.
Just a quick progression from that: What if I needed to filter by a second custom field?
So I tried repeating the array('meta_key' => like this:

$children = toolset_get_related_posts(
$parent->ID,
$relationship_slug,
'parent',
1000000,
0,
array('meta_key' => 'wpcf-your-field-slug',
'meta_compare' => '=',
'meta_value' => '1'),
'post_id',
'child'
array('meta_key' => 'wpcf-your-other-field-slug',
'meta_compare' => '=',
'meta_value' => '1'),
'post_id',
'child'
);

But yeah... that didn't work of course...
I apologise I am not savvy with PHP. I wish I was! One day...

#2008113

Minesh
Supporter

Languages: English (English )

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

Here is the example of how you can use it to compare two custom fields:

$children = toolset_get_related_posts(
$parent->ID,
$relationship_slug,
'parent',
1000000,
0,
array(
array('meta_key' => 'wpcf-your-field-slug',
      'meta_compare' => '=',
      'meta_value' => '1'),
array('meta_key' => 'wpcf-other-field-slug',
      'meta_compare' => '=',
      'meta_value' => '1')
),
'post_id',
'child'
);
#2008169

Hmmmm that doesn't work.
I tried with 2 x 'wpcf-field-slug' custom fields and a combination of one 'wpcf-field-slug' field and the woocommerce field: '_stock field' is '>=' to '1'
If I swap it back to compare just one field as per your code earlier on any of these fields work fine.
Sorry Minesh!
Maybe I have I done something wrong..?

#2008171

Minesh
Supporter

Languages: English (English )

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

Can you please share details which field you want to compare and where you added the code I shared with 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.

#2008219

Minesh
Supporter

Languages: English (English )

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

I will require FTP access details in order to edit the functions.php file.

Can you please send me FTP access details.

I have set the next reply to private which means only you and I have access to it.

#2008241
Screenshot 2021-04-02 at 10.30.02.png

Minesh I have a critical error on various pages.
I need to know what you have done so I can quickly undo it.
The site is live.

#2008261

Minesh
Supporter

Languages: English (English )

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

I can see the site is back working again: hidden link

Please send me FTP access details.

I have set the next reply to private which means only you and I have access to it.

#2008263

That's because I quickly and temporarily pulled the 'event-grid' view out of the template.
It is also happening on hidden link

#2008265

Minesh
Supporter

Languages: English (English )

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

It seems you revoke all rights from me as I could not access the admin section now.

Please let me know once you done with your changes as I tried to make change to functions.php file but I got error that changes I made will not be reflected and it will revert back automatically.

#2008277

Hi Minesh
I would be grateful if you could quickly tell me what you did while you were logged in to the site so that I can resolve the critical error.
Many thanks!
Rita

#2008281

Minesh
Supporter

Languages: English (English )

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

I tried to move your view's filter hook from the functions.php and try to add that to the "Custom Code" section offered by Toolset.

You may find a file namely "toolset-custom-code.php" at path wp-content/toolset-customizations.
You should try to remove that file and see if everything get back to normal.

#2008293

Great thank you! I removed it. The site is restored.
That is why I don't use the Toolset 'toolset-custom-code.php'.... 😉
I am sorry I logged you out quickly without warning. It was only to be sure that there was only one active administrator (me) to fix the problem because it wasn't clear in that moment what had caused it you see?
So. All is well. How would you like to proceed?

#2008301

Minesh
Supporter

Languages: English (English )

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

I would like to inform you that with meta_query argumentsd only LIKE and '=' are supported.

Please check:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

meta_key, meta_value and meta_compare: Works exactly like in WP_Query. Only limited values are supported for meta_compare ('=' | 'LIKE').

That means you can not use '>=' with meta_compare argument.

I need FTP access details and let me see if I can able to offer you any workaround.

I have set the next reply to private which means only you and I have access to it.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.