获取WordPress的所有帖子的ID(Get all post IDs in Wordpress)

2019-09-21 03:58发布

是否有可能获得当前存在于WordPress的DB(不论post_types)的所有文章ID数组? 此外,有可能得到一个特定的post_type的所有文章ID数组?

如果能,如何做到这一点?

Answer 1:

你可以试试这个方法

    $post_ids = get_posts(array(
        $args, //Your arguments
        'posts_per_page'=> -1,
        'fields'        => 'ids', // Only get post IDs
    ));


Answer 2:

可能是最好的运行使用的WordPress的数据库对象的自定义查询。 (从的functions.php或主题文件等):

                // pseudo-code check how to refer to the field columns and table name!
                global $wpdb; 

                $sql="SELECT id, title FROM posts";

                $posts = $wpdb->get_results($sql);

                print("<ul>");
                foreach ($posts as $post)
                {
                    print('<li>'.$post->FIELD1.'|'.$post->FIELD2.'<br/>');
                     print('</li>');
                }
                print("</ul>");

我想其实你可以得到还可与标准wp_query对象....但至少我的方式,你可以使在phpMyAdmin查询,然后再调整为语法/ WordPress的前缀。 (读DB对象的抄本)。 如果这是一个一次性的只是用phpmyadmin,但程序中使用,你应该然后将其转换从您的functions.php文件运行。



文章来源: Get all post IDs in Wordpress