Skip Navigation

[Resolved] How to check if the parent post is empty

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to check if their post parent is empty.

Solution:
This can be done with a little custom code.
Sure i'll just modify the code a little so that it can work for your case.

Add the shortcode below to your site.

// Add Shortcode
function wp_post_has_content( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'relationship' => '',
        ),
        $atts
    );
 
       $return = 0;
       $postid = do_shortcode("[wpv-post-id item='".$atts['relationship']."']");
       $object = get_post($postid);
        
        if ( empty( $object->post_content ) ) {
            // return 0 when post's content is empty
            $return = 0;
        } else {
             // return 1 when post's content is not empty
            $return = 1;
        }
        return $return;
 
}
add_shortcode( 'has_content', 'wp_post_has_content' );

Add this shortcode to the Toolset custom code section in Toolset -> Settings -> Custom Code. Then from there you need to add the shortcode name "has_content" to 3rd party shortcode arguments in Toolset->Settings->Frontend.

The usage of the code is

[has_content  relationship= "@nemovitost-spolecnosti.parent"]

From there you should be able to get the content information.

You can use this shortcode in a conditional as well.

This support ticket is created 5 years, 4 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by toolset-dave 5 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1322955

Hi,

I am using this function to check if the post content is empty or not: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/#checking-for-empty-post-content

But now I need to check it on child post.

I have tried this, but it is not working:

[wpv-conditional if="( $(wpv_conditional_post_has_content()).item(@nemovitost-spolecnosti.parent) eq '1' )"]A[/wpv-conditional]

[wpv-conditional if="( $(wpv_conditional_post_has_content()).item(@nemovitost-spolecnosti.parent) eq '0' )"]B[/wpv-conditional]

#1323177

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Thank you for getting in touch.

Is it that you want to check if the post has a parent or if the post Parent has content ?

As this function won't work for your case if is to check if the post parent has any content.

Thanks.
Shane

#1323689

Hi Shane,

I want to check if the post Parent has content. So can you help me with the function?

Thank you.

#1324379

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

This is going to require some custom code to achieve.

However if you can let me know a little more information. Is it possible for the child to be attached to a parent that doesn't have any post content ?

This is because if the child doesn't have a parent then we can check for the parent ID .

Please let me know and we can go from there.

Thanks,
Shane

#1324431

Hi Shane,

yes, it is possible to attach child to parent which doesn´t have any post content.

#1324475

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Sure i'll just modify the code a little so that it can work for your case.

Add the shortcode below to your site.

// Add Shortcode
function wp_post_has_content( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'relationship' => '',
		),
		$atts
	);

	   $return = 0;
	   $postid = do_shortcode("[wpv-post-id item='".$atts['relationship']."']");
	   $object = get_post($postid);
	   
	    if ( empty( $object->post_content ) ) {
	        // return 0 when post's content is empty
	        $return = 0;
	    } else {
	         // return 1 when post's content is not empty
	        $return = 1;
	    }
	    return $return;

}
add_shortcode( 'has_content', 'wp_post_has_content' );

Add this shortcode to the Toolset custom code section in Toolset -> Settings -> Custom Code. Then from there you need to add the shortcode name "has_content" to 3rd party shortcode arguments in Toolset->Settings->Frontend.

The usage of the code is


[has_content  relationship= "@nemovitost-spolecnosti.parent"]

From there you should be able to get the content information.

Please let me know if this helps.
Thanks,
Shane

#1325123

My issue is resolved now. Thank you Shane!