Get all post IDs in Wordpress

允我心安 提交于 2020-01-03 14:15:16

问题


Is it possible to get an array of all post IDs currently present in the wordpress DB (irrespective of post_types)? Also, is it possible to get an array of all post IDs of a specific post_type?

If we can, how to achieve that?


回答1:


You can try this way

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



回答2:


probably best to run a custom query using the DB object of wordpress. (from functions.php or a theme file etc):

                // 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>");

I think in fact you can get that also with standard wp_query object.... but at least my way you could make the query in phpmyadmin first, then adjust for the syntax/wordpress prefix. (read the codex on DB object) . If it is a one-off just use phpmyadmin, but for programmatic use you should then convert it to run from your functions.php file.



来源:https://stackoverflow.com/questions/11937683/get-all-post-ids-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!