0

hope someone of you can help me. On my wordpress-page, i get the following messages, when I use the search function and there are no results:

Notice: Trying to get property of non-object in /var/www/vhosts/tronicmart.de/httpdocs/wp-content/themes/oceanwp/inc/helpers.php on line 3392

Notice: Trying to get property of non-object in /var/www/vhosts/tronicmart.de/httpdocs/wp-content/themes/oceanwp/inc/helpers.php on line 3400

Notice: Trying to get property of non-object in /var/www/vhosts/tronicmart.de/httpdocs/wp-content/themes/oceanwp/inc/helpers.php on line 3406

    if ( ! function_exists( 'oceanwp_excerpt' ) ) {

    function oceanwp_excerpt( $length = 30 ) {
        global $post;

        // Check for custom excerpt -->line 3392
        if ( has_excerpt( $post->ID ) ) {
            $output = $post->post_excerpt;
        }

        // No custom excerpt
        else {

            // Check for more tag and return content if it exists -->line 3400
            if ( strpos( $post->post_content, '<!--more-->' ) ) {
                $output = apply_filters( 'the_content', get_the_content() );
            }

            // No more tag defined -->line 3406
            else {
                $output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
            }

        }

        return $output;

    }
}

1 Answer 1

0

If there are no search results, then it is somewhat expected that the $post variable is empty and there are no properties in it to be used in the if statements.

You can prevent the error by adding an if statement to make sure $post is what it is supposed to be before the line 3392.

if ( ! is_object($post) || ! is_a($post, 'WP_Post') ) ) { return; }

Or the lazy way if ( empty( $post->ID ) ) { return; }

You can always use var_dump to check / debug your variables to make sure they contain the stuff you need.

4
  • On the other hand, if there is no post, then no excerpt should be printed? So your fix is only a cover up for a larger error... Commented Feb 24, 2019 at 7:25
  • I tried both of your code snippets and the second one works fine for me. Thank a lot!
    – TronicM
    Commented Feb 24, 2019 at 13:26
  • @KrzysiekDróżdż, I understood that the error arises when there are no search results. In that situation there would be no $post object and trying to use its properties in the if statements would cause the errors as $post is null, right? Of course, in the case when there should be a $post and there isn't one, then the proposed fix will just mask the underlying problem. Commented Feb 24, 2019 at 15:01
  • @TronicM, you're welcome. If you want, you can mark my answer asvaccepted to let others know that your question has been solved. Commented Feb 24, 2019 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.