Skip Navigation

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

This support ticket is created 3 years, 7 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
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, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2008475

Minesh
Supporter

Languages: English (English )

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

I could not connect to the FTP. Can you please share if I need to use any specific port or send me working FTP/SFTP access details.

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

#2008565

Hi Minesh
Yes sorry I just saw your message! Its port 21 - and IP 141.0.151.26
Let me know how you get on?
Rita

#2010267

Minesh
Supporter

Languages: English (English )

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

Looks like there is some issue with your FTP server.

I can access the file but after making changes to the functions.php file when I try to upload it, it shows me the following server error:

Error:	Connection timed out after 20 seconds of inactivity
Error:	File transfer failed

Can you please make sure that I should be able to upload the file back to the server.

#2011925

Hi Minesh
Apologies. I am not sure why you weren't able to edit the functions.php file. I had tested the FTP credentials I sent you before sending them to you and they worked fine for me... Not to worry. I have resolved the issue of the missing file and restored the site once again.
Perhaps we should change tactics? 😉
If you are happy to send me your edited functions.php file or at least the code you have added/edited I can pop it in and test it for you?
Rita

#2013295

Minesh
Supporter

Languages: English (English )

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

Before I pass the file to you, I would like to test it first to check if I'm getting the right results. However - I again checked and FTP access details is not working at this end.

If you want here is the modified code - you should try to check and see if that helps you to resolve your issue.:

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(
		array('meta_key' => 'wpcf-ff-post-status',
      		'meta_compare' => '=',
      		'meta_value' => '1'),
		array('meta_key' => '_stock',
      		'meta_compare' => '>=',
      		'meta_value' => '1')
		),
		'post_id',
		'child'
		);
		
		*/
		
		$query = new WP_Query( 
						 array(
						  'post_type' => 'ticket',
						  'fields'=>'ids',
						  'posts_per_page' => -1,
						  'meta_query' => array(
								array('key' => 'wpcf-ff-post-status',
									  'compare' => '=',
									  'value' => '1'),
								array('key' => '_stock',
									  'compare' => '>=',
									  'value' => '1')
							
						   ),
						  //new toolset_relationships query argument
						  'toolset_relationships' => array(
						   'role' => 'child',
						   'related_to' => $parent->ID,
						   // this will work only with relationships that have existed before the migration
						   // if possible, use the relationship slug instead of an array
						   'relationship' => $relationship_slug
						  ),
						  
						 )
						);
			$children = $query->posts;
			
			
		
		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;
}
#2014941

Hi Minesh

I am really sorry you can't access the functions.php file via FTP. I am consulting with my 'server guy' to see what the problem is.

Also, FYI, I have since ran the Toolset Relationships table update which I had been reluctant to do since it made a little mess of another website I have... But on this site it seems to have updated fine. Certainly, I haven't found any problems yet.

In the meantime, I have dropped your code in for you, specifically this section:

$query = new WP_Query(
array(
'post_type' => 'ticket',
'fields'=>'ids',
'posts_per_page' => -1,
'meta_query' => array(
array('key' => 'wpcf-ff-post-status',
'compare' => '=',
'value' => '1'),
array('key' => '_stock_status',
'compare' => '=',
'value' => 'instock')
),
//new toolset_relationships query argument
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $parent->ID,
// this will work only with relationships that have existed before the migration
// if possible, use the relationship slug instead of an array
'relationship' => $relationship_slug
),
)
);
$children = $query->posts;

But unfortunately it renders a 'critical error' as it did before.

Also, just to mention, if this new array idea is about working around the '<=' comparison versus '=' comparison then we can also use the _stock_status field instead if you like. So I reverted back to the previous format and tried this as follows:

$children = toolset_get_related_posts(
$parent->ID,
$relationship_slug,
'parent',
1000000,
0,
array(
array('meta_key' => 'wpcf-ff-post-status',
'meta_compare' => '=',
'meta_value' => '1'),
array('meta_key' => '_stock_status',
'meta_compare' => '=',
'meta_value' => 'instock')
),
'post_id',
'child'
);

But no... it doesn't work either. It doesn't seem to filter results by the second comparison. If I swap the order and put the _stock_status comparison first then it filters by that and not the wpcf-ff-post-status comparison.

What do you reckon? 🙂

Let me know how you would like to tweak it and I will pop it in for you quick as a flash. No problem.

Rita

#2015897

Hello Rita, Minesh won't be available for a couple of days, if you don't mind, I'll continue with you on this ticket.

Maybe you need to add the realtion to the meta_query array:

'meta_query' => array(
                                'relation' => 'AND',
                                array('key' => 'wpcf-ff-post-status',
                                      'compare' => '=',
                                      'value' => '1'),
                                array('key' => '_stock',
                                      'compare' => '>=',
                                      'value' => '1')
                             
                           ),

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
However, AND is the default value, so, this should not be required.

If this does not help, can you provide more details about your use case? Where this code is implemented? Where it is being used? Please provide a test scenario(go/do xxx, expected to get yyy, actual results zzz).
I am sure you will understand that I am not familiar with your website. By providing these details, I won't spend too much time guessing what would be expected.

#2016883
compare with 2.png
compare with 1.png

Hi Jamal
Yes, that's absolutely fine. I hope Minesh is ok!
So I added your 'relation' => 'AND', idea but unfortunately there is already something amiss with Minesh's code which causes a critical error...
Here is the background on the use case as requested:

I am using Toolset and Woocommerce...

POST RELATIONSHIP
Parent post called 'Event'.
Child post called 'Ticket' (woocommerce product).

CUSTOM FIELD: 'wpcf-ff-post-status'
This is a toolset select field that is used by both 'events' and 'tickets' and has many values including the raw value of '1'.
We only want to display parent events and child tickets that have a value of '1'.

CUSTOM FIELD: '_stock_status'
This is a woocommerce field, is used by child tickets, and has 2 raw values, either 'instock' or 'outofstock'.

TOOLSET VIEW
Ideally the view would display parent events:
IF the parent event 'wpcf-ff-post-status' = '1'
AND IF the parent event has child tickets
AND IF the child ticket 'wpcf-ff-post-status' = '1'
AND IF the child ticket '_stock_status' = 'instock'

The view filters events by 'wpcf-ff-post-status' = '1' and then I was using the function to do the child tickets filter bit.

Minesh and I had established that we can compare with 1 of the fields (see 'compare with 1' attached) but if we wrap it in an array and add a second field then neither comparison works (see 'compare with 2' attached).

So then Minesh had a go at another idea - see above thread - but as you see it causes an error.

I hope this helps clarify!

Please let me know if you need any more information.

Thanks in advance.

Rita

#2017119

Thank you, Rita. To better assist you with this issue, I'll need to run some tests and maybe I'll need to debug locally. Would you like to reproduce this use case on one of our test sites or allow me to take a copy of your website and test it locally?

#2017127

Hi Jamal
I am happy to reproduce the scenario for you on your test site if you can instruct me on how to access and make use of that facility.
Thank you
Rita

#2017433

Thank you, Rita.

You can log in to our test site using this one-click-link hidden link
All the Toolset plugins are updated and installed. Please create the required custom post types and relationships, and the needed posts and views for testing.

#2021167

Hi Jamal
I have discussed it with my client and agreed to go in another direction.
So we don't need to pursue a resolution to this problem anymore.
Just to thank you and Minesh for your support.
I really appreciate it.
Rita