How to ignore WP_ERROR caused by "get_the_excerpt" method in an AJAX call?
function get_post_data(){
$post_id = $_POST['post_id'];
...
$data['title'] = get_the_title( $post_id );
...
$data['excerpt'] = get_the_excerpt( $post_id );
...
echo json_encode( $data );
wp_die();
}
The above code shows the scheme of the ajax call. Some data should be retriven from a post given by its ID outside the LOOP. This seems to work when the content fields like title and excerpt not empty. But when empty I get an Internal Server Error (500: admin-ajax.php not found). When I debug the PHP code it seems an WP_ERROR to be thrown after calling get_the_excerpt.
I just wanted to get some kind of solution like:
If (get_the_excerpt( $post_id ) returns an WP_ERROR) then let $data['excerpt'] = '';
Anything I tried f.e. try{} catch{} won't work, also not when I insert this to initialize the loop:
setup_postdata($post_id);
in the second line of the get_post_data function.
(EDIT) RESOLVED:
The problem was caused by a conflict with the DIVI theme I used. When I followed the error in the WP_ERROR class I got:
Uncaught Error: Call to undefined function et_builder_init_global_settings() in [websitefolderpath]\wp-content\themes\Divi\includes\builder\feature\gutenberg\blocks\Layout.php:228
And this is getting relevant in the get_the_excerpt method in the following line:
return apply_filters( 'get_the_excerpt', $post-post_excerpt, $post );
After updating DIVI to the last version the error no longer exists.
Maybe I won't use get_the_excerpt() method in future in this project because it maybe can have many side effects causing strange results or errors.
Topic wp-error get-the-title ajax Wordpress
Category Web