Get all post IDs in WordPress

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?

Related posts

Leave a Reply

2 comments

  1. 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.