$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - How to know if get_posts() failed?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - How to know if get_posts() failed?

matteradmin8PV0评论

So I see that get_posts() function return an array of posts.

  1. If there are posts, it returns an array of posts.

  2. If there are no posts it returns an empty array().

  3. If there are posts, but the querying of the database fails (and let's say, 99.999% times it returns correct, but this time it fails for some reason), it returns an empty array. (Important: I'm not sure of this, this is what I understand when I read how this function works).

My question is this: If I use this function, when do I know if the function failed? (Like there are some functions which returns an WP_Error.)

So I see that get_posts() function return an array of posts.

  1. If there are posts, it returns an array of posts.

  2. If there are no posts it returns an empty array().

  3. If there are posts, but the querying of the database fails (and let's say, 99.999% times it returns correct, but this time it fails for some reason), it returns an empty array. (Important: I'm not sure of this, this is what I understand when I read how this function works).

My question is this: If I use this function, when do I know if the function failed? (Like there are some functions which returns an WP_Error.)

Share Improve this question edited Feb 16, 2019 at 10:20 tmdesigned 1,6751 gold badge14 silver badges17 bronze badges asked Feb 16, 2019 at 10:00 BradaBrada 1828 bronze badges 2
  • What kind of failing do you mean? Like connection error? – Krzysiek Dróżdż Commented Feb 16, 2019 at 10:51
  • Hi... I can't say exactly what kind of failing, because I really don't know what can fail, but I think there should be something that can fail. Sure, a connection is very likely to happen if the database is somewhere else... It's just strange that there is nothing to indicate if it's a fail or not, like a lot of other functions that do. Just look at a function like wp_insert_post(), has even a second argument to either return WP_Error on failure or not. – Brada Commented Feb 16, 2019 at 19:17
Add a comment  | 

1 Answer 1

Reset to default 2

Here's one way to check if there was an DB error within get_posts():

global $EZSQL_ERROR;

$before = isset( $EZSQL_ERROR ) ? count( $EZSQL_ERROR ) : 0;
$posts  = get_posts( $args );
$after  = isset( $EZSQL_ERROR ) ? count( $EZSQL_ERROR ) : 0;

if ( empty( $posts ) && $before < $after ) {
    // ... DB error(s) within get_posts() when it returns an empty array.
}

Here we check the number of wpdb errors before and after the get_posts() call from the global $EZSQL_ERROR errors collector (src).

But I can imagine that this might give a false positive in some cases though, e.g. if we hook a bad db calls within get_posts() that might not be the reason for empty posts array.

Update. I tested this and noticed that $wpdb->last_error is restored after each $wpdb call. I noticed the global $EZSQL_ERROR array within wpdb::print_error() that is not restored but collects the errors. I therefore updated the answer and replaced $wpdb->last_error with $EZSQL_ERROR.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far