簡體   English   中英

獲取所有在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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM