$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'); ?>How to get posts without author?|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)

How to get posts without author?

matteradmin9PV0评论

I am looking for a way to get all posts without author.

They were inserted with post_author = 0;

But the following line returns all posts instead of the posts with no/null author

get_posts( array('author' => 0 ));

How to get posts without author?

I am looking for a way to get all posts without author.

They were inserted with post_author = 0;

But the following line returns all posts instead of the posts with no/null author

get_posts( array('author' => 0 ));

How to get posts without author?

Share Improve this question asked Feb 25, 2019 at 7:12 RafaSashiRafaSashi 5071 gold badge7 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

That won't be as easy as you'd like to... get_posts uses WP_Query to get posts and if you take a look at WP_Query code, ten you'll see, that 0 is used as empty in there (https://core.trac.wordpress/browser/tags/5.0.3/src/wp-includes/class-wp-query.php#L2052):

if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {

It means, that you can't pass 'author' => 0 and get posts that have 0 as author. And that's correct behavior, because 0 is not a valid value for post_author column - this column should contain ID of existing user.

On the other hand, if you already have 0s in there, then you'll have to use custom SQL to get these posts. One way to do this will be with this code:

global $wpdb;
$ids = wp_list_pluck( $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type=post AND post_author=0" ), 'ID' );
$posts = get_posts( array( 'post__in' => $ids, 'posts_per_page' => -1 ) );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far