Skip Navigation

[Resolved] Using Conditional Logic in Determining if Post Body is Empty or Not

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 4 replies, has 3 voices.

Last updated by zacharyL 1 year, 2 months ago.

Assigned support staff: Jamal.

Author
Posts
#2177705
screenshot.jpg

I'm trying to use conditional logic within a custom post type's archive. If the post body is empty, I'd like to link to the URL associated with a custom field, however if there is content in the post body, I just want it to simply link to the post.

I realize there have already been posts about this exact subject, however even after following the directions provided in your documentation, here: https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/, the loop output only wants to output as if there's nothing in the post body.

This is my markup:

[wpv-conditional if="( wpv_conditional_post_has_content() eq '0' )"]
    <a href="[types field='document-or-external-link' output='raw'][/types]" class="resource" target="_blank" aria-label="view [wpv-post-title]">
        [wpv-conditional if="( '[wpv-post-featured-image output='url']' ne '' )"]
            <img src="[wpv-post-featured-image size='full' output='url']" alt="[wpv-post-title]" />
        [/wpv-conditional]
        <div class="resource-title">
            <h3>[wpv-post-title]</h3>
        </div>
    </a>
[/wpv-conditional]
[wpv-conditional if="( wpv_conditional_post_has_content() eq '1' )"] 
    <a href="[wpv-post-url]" class="resource" aria-label="view [wpv-post-title]">
        [wpv-conditional if="( '[wpv-post-featured-image output='url']' ne '' )"]
            <img src="[wpv-post-featured-image size='full' output='url']" alt="[wpv-post-title]" />
        [/wpv-conditional]
        <div class="resource-title">
            <h3>[wpv-post-title]</h3>
        </div>
    </a>
[/wpv-conditional]

Switching

eq '1'

to

ne '0'

doesn't seem to affect anything differently.

Here's my function:

// Determine if post content is empty or not

function wpv_conditional_post_has_content($type, $object) {
    $return = 0;
    if ( $type == 'resource' ) {
        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;
}

It's really just copied directly from the documentation, but with the post type changed from "posts" to "resource". And yes, I registered the function with the "Functions inside conditional evaluations" setting. Please see attached.

The archive page in question is hidden link

The Resource labeled "Volunteer Information" should link to the post itself, as it does not have a file associated with my custom field, and its post body is populated with content. None of the other posts in this output have content in their bodies.

#2178477

Nigel
Supporter

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

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

You shouldn't have to declare custom functions to check for an empty post body, you can do it directly within the normal Conditional block or wpv-conditional shortcode.

It seems you are using shortcodes, in which case the conditions would like...

[wpv-conditional if="( empty( '[wpv-post-body view_template='None' ]') )"]

and

[wpv-conditional if="( NOT ( empty( '[wpv-post-body view_template="None" ]') ) )"]

One thing to be wary of: if the posts are edited with the block editor then they can appear empty but actually contain some content added by Gutenberg, in which case checking if the post content is empty will return false.

All you have to do when editing a post with the Gutenberg editor to insert some "blank" content is click in the content editor, just so you are aware.

#2178861

Good thing we're not using the Gutenberg editor, then.

Your solution did work, however, so thank you so much for your help in this regard.

That said, why do you suppose the Toolset documentation is giving wrong or outdated information? It would have saved me time if it presented the solution you gave me as an option, given that the conditional logic options don't even include a selection for the post body shortcode.

#2179379

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

This may seem controversial, but I think that the example in our documentation is not correct. The custom function "wpv_conditional_post_has_content" expects to have two arguments "$type" and "$object", but they are not passed to it during the condition:

wpv_conditional_post_has_content($type, $object)

Nigel is also right, the content may contain some hidden markup from Gutenberg. The content may alos come from a plugin or the theme if they use the "the_content" filter. Please try this code:

function wpv_conditional_post_has_content() {
    gloabl $post;
    $return = 0;
    if ( $post->post_type == 'resource' ) {
        // let's remove HTML markup
        $post_content = strip_tags( $post->post_content );
        if ( empty( $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;
}

If this does not help, please allow me temporary access to your website and let me check this closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2182135

I resolved the issue with Nigel's recommended fix. Since we're not using Gutenberg, I don't imagine we'll have any issues with invisible markup.

I was just wondering why the fix in the documentation was ineffective, but Nigel's was.

Thanks guys, for your time.