获取 Wordpress 中所有已发布的帖子,由于结果集过长导致的性能问题

Get all published posts in Wordpress, performance issues due to long result set

我需要像这样构建一个数组:

$arrPosts = array(
    thePostID => "The Post Title"
)

我正在做的事情如下:

$posts = get_posts(
    array(
        'numberposts' => -1,
        'post_status' => 'published',
        //'post_type' => get_post_types('post', 'opinion')
    )
);

foreach($posts as $post) {
    $article[] = [
        $post->ID => $post->title
    ];
}

但是处理的时间很长,不太合适(我不得不设置define('WP_MAX_MEMORY_LIMIT','1024M'),太占内存了)。我只需要从 post 和意见自定义 post 类型中获取 post。

有没有人知道实现此目的的更好方法?

你可以这样使用wp_query()

$args = array(
              'post_type' => 'post',
              'orderby'   => 'title',
              'order'     => 'ASC',
              'post_status' => 'publish', //here you can retrieve posts that are published
              'posts_per_page' => -1,
            );

// The Query
$the_query = new WP_Query( $args );
$posts = array();
// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $posts['thePostID '] =  get_the_title() ; //change appropiately
    }

} else {
    // no posts found
}
print_r($posts);
/* Restore original Post Data */
wp_reset_postdata();