YashG99
YashG99

Reputation: 529

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?

Upvotes: 5

Views: 10790

Answers (2)

Mayur Chauhan
Mayur Chauhan

Reputation: 731

You can try this way

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

Upvotes: 6

Luke Barker
Luke Barker

Reputation: 915

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.

Upvotes: 3

Related Questions