Author is Aleš Sýkora
Updated: 20. 9. 2022, Added: 29. 4. 2022
Check if current post has child in WordPress
When you are in situation, where you want to check if current post has a child posts, then you need to add custom function. It's the easiest way how to do it.
Check if post has childs programatically
<?php
function great_tit_babysitter()
{
global $post; //load current post variables
return count(
get_posts(
array(
'post_parent' => $post->ID,
'post_type' => $post->post_type
)
)
);
}
You can add this function to your code snippets plugin or custom funcionality plugin (I am using Advanced Scripts plugin).
After that, you can finally go to Oxygen Builder and use the function. I will use it for a conditional. So click to create a conditional > select a PHP Function Return Value > fill the function name.
I am using this function to conditionally display section with child posts of current post. The complete condition in Oxygen looks like this: