获取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')这是很多内存)。我只需要从帖子和意见自定义帖子类型中获取帖子。

有谁知道实现这一目标的更好方法?

你可以

像这样使用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();