Aleš Sýkora / November 28, 2023 / 0 comments

Check if current post has child in WordPress

Post summary: 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 You can add this function to your code snippets plugin or custom funcionality plugin (I am using…

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.

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts