Home › Toolset Professional Support › [Resolved] Only display parent posts that have children with a custom field value
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)
Tagged: Post-relationship, Types fields API, Types plugin
Related documentation:
This topic contains 26 replies, has 3 voices.
Last updated by Rita 3 years, 7 months ago.
Assisted by: Jamal.
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
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?
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...
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' );
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..?
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.
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.
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.
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.
That's because I quickly and temporarily pulled the 'event-grid' view out of the template.
It is also happening on hidden link
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.
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
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.
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?
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.