Skip Navigation

[Resolved] Function types_child_posts() doesn't work when the child is a "connecting type".

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 11 replies, has 2 voices.

Last updated by Tim 7 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#402952

Tim

Hi. I'm a bit puzzled by this.

When using the function types_child_posts() it works fine for normal children. However when checking for the existence of a "connecting child" in the case of many-to-many relationships it returns nothing.

As it is still just a child I thought it would work, but returns nothing.

#402997

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I quickly tested this on a site where I had used "gigs" as an intermediate child post to connect "comedians" and "venues" and it worked as expected.

The documentation is here:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

You need to remember to specify the post type of your intermediary. In my case that meant the following:

$child_posts = types_child_posts("gig");
#403008

Tim

Hi Nigel, thanks for the reply and I believe you, but...

I'm looping through parents and using a custom shortcode to evaluate whether the parent has a child. I have Council -> Tender Connection <- Joint Tender many to many relationships.
and Council -> Tender in a Parent -> child relationship. So Tenders and Joint Tenders are quite separate.

The shortcode is:

// has-child-posts shortcode

function child_posts_exist_func(){
    
    $child_posts = types_child_posts('tender-connection');
    if ($child_posts) {
        return 1;
    } else {
        return 0;
    }
}
add_shortcode( 'has-child-posts', 'child_posts_exist_func' );

And the view has this:

	
<wpv-loop> 
      [wpv-conditional if="( '[has-child-posts]' = '0' )" evaluate="false"] 
<div class="">[wpv-post-body view_template="Loop item in View for Councils"]</div>       
      [/wpv-conditional]
	</wpv-loop>

That works fine with

types_child_posts('tender');

The councils with Tenders show and the others don't.

As soon as I change the function it to

types_child_posts('tender-connection');

Nothing displays, despite there being several Joint Tenders to return '1'.

Not critical to my website but just don't understand why this is the case.

#403009

Tim

Clarify:
Nothing displays, despite there being several Joint Tenders to return '1'.

Nothing displays, despite there being several Joint Tenders with the necessary Connections to return '1'.

#403076

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I can't readily see why this wouldn't work.

In each case the parent is 'council', yes? And your view template is not dependent upon the current item in the loop being a tender?

Could you add some dummy content to check what is happening? Something like:

<wpv-loop>
	<h2>has-child-posts = [has-child-posts]</h2>
	[wpv-conditional if="( '[has-child-posts]' = '0' )" evaluate="false"] 
		<h3>Inside the conditional</h3>
		<div class="">[wpv-post-body view_template="Loop item in View for Councils"]</div>       
	[/wpv-conditional]
</wpv-loop>

Let me know what you see...

#403084

Tim

This is for types_child_posts('tender');. In the last (Wakefield Regional Council) you see joint tenders mentioned as that council has both tenders and joint tenders.

has-child-posts = 0
has-child-posts = 0
has-child-posts = 0
has-child-posts = 1
Inside the conditional
Port Pirie Regional Council

Tender Number 1
Tender Number 2
Tender Number 4

No joint tenders at the moment.
has-child-posts = 1
Inside the conditional
Wakefield Regional Council

Tender Number 3

No joint tenders at the moment.
has-child-posts = 1
Inside the conditional
District Council of Yorke Peninsula

Tender Number 5

Joint Tender 2 (Joint Tender)
Joint Tender 1 (Joint Tender)

'tender-connection' produces the following, multiplied by the total number of councils.

has-child-posts = 0
has-child-posts = 0
has-child-posts = 0

#403095

Tim

In each case the parent is 'council', yes?
Yes.
And your view template is not dependent upon the current item in the loop being a tender?
No, the query (of councils) is not filtered, it loops through all the councils. There are two views in the "Loop item in View for Councils" each one relies on the "Select posts that are a children of the current post in the loop."

#403097

Tim

Sorry, not Wakefield Regional Council, Yorke Pen...

#403122

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I did some more testing and it still works for me, so there must be something slightly awry with your set up.

As I already had my comedians test site available I persevered with that, if you'll bare with me.

So, my CPTs are Comedians, which have child CPTs of Shows and Gigs. Gigs is the intermediary post type to connect Comedians to a CPT of Venues in a many-to-many relationship.

(From my earlier test I just added Shows as a child post of Comedians to match your structure.)

I created some actual content: several comedians, venues, gigs, and I added shows for Ricky Gervais and Jerry Seinfeld only.

I created custom shortcodes as per your code, though I made one each for gigs and shows.

function child_posts_gig_func() {
	$child_posts = types_child_posts("gig");
    if ($child_posts) {
        return 1;
    } else {
        return 0;
    }
}
add_shortcode( 'has-child-posts-gig', 'child_posts_gig_func' );

function child_posts_show_func() {
	$child_posts = types_child_posts("show");
    if ($child_posts) {
        return 1;
    } else {
        return 0;
    }
}
add_shortcode( 'has-child-posts-show', 'child_posts_show_func' );

Now, I created a View to show comedian posts with no filters, so it is going to loop through each of my comedians.

My loop output section looks like this:

<wpv-loop>
	<h2>[wpv-post-title]</h2>

	<h3>gig check: [has-child-posts-gig]</h3>
	[wpv-conditional if="( '[has-child-posts-gig]' = '0' )" evaluate="false"] 
		<h4>has gigs</h4>
	[/wpv-conditional]
	<h3>show check: [has-child-posts-show]</h3>
	[wpv-conditional if="( '[has-child-posts-show]' = '0' )" evaluate="false"] 
		<h4>has shows</h4>
	[/wpv-conditional]  
</wpv-loop>

Add added this view to a page, and here are my results:
Chris Rock
gig check: 1
HAS GIGS
show check: 0

Ricky Gervais
gig check: 1
HAS GIGS
show check: 1
HAS SHOWS

Jerry Seinfeld
gig check: 1
HAS GIGS
show check: 1
HAS SHOWS

Louis CK
gig check: 1
HAS GIGS
show check: 0

That's exactly what I was expecting.

Can you spot the difference, and double-check for typos etc?

If you still can't identify the problem let me know.

#403204

Tim

Thanks Nigel. Sorry to put you to that trouble.

The only typo could be the child slug one works and the other doesn't, and I don't see how I could misspell "tender-connection" as that's the only text I'm changing from one scenario to the other. ?

Anyhow, knowing that it *should* work is really what I needed to know ?

I'll keep an eye out for any anomalies and post back if I find something.

Thanks heaps again.
T

#403256

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Well, you said you didn't actually need it to work, but if you want to get it working, I'd suggest you maybe start again and reproduce what I did by way of a test and see if that works and hopefully that will reveal something you have done differently.

#403261

Tim

Yes, that's correct, it is a display that I MAY use, and will try again then, as well as knowing what can be done. After all that's what make these plugins so cool, being aware of what can be achieved with them.

Thanks for bearing with me!
Best regards, Tim

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