Friday, April 29, 2022
The Great Tit
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).

Add the code to your code snippets 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.

Call the function from Oxygen Builder

I am using this function to conditionally display section with child posts of current post. The complete condition in Oxygen looks like this:

Oxygen Builder conditional - display section if post has childs.
Did I help you? Support me on Patreon!
Beep this article to your friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram