Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all post IDs in Wordpress

Tags:

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?

like image 414
YashG99 Avatar asked Aug 13 '12 15:08

YashG99


People also ask

How do I print a WordPress post ID?

Get Post ID Using the get_the_ID() and the_ID() Functions The get_the_ID() and the_ID() functions display the current post's ID. The main difference between the two functions is that get_the_ID() returns the post ID without displaying it, while the_ID() always prints the post ID.

How can I get post ID?

This Post ID is a string of numbers at the end of a Facebook post's URL. The easiest way to find your Facebook post's direct URL is to go to the post itself in your Facebook Business Page newsfeed, and right-click on the date/timestamp on the post and open it in a new tab to go to the direct link of the post.


2 Answers

You can try this way

    $post_ids = get_posts(array(
        $args, //Your arguments
        'posts_per_page'=> -1,
        'fields'        => 'ids', // Only get post IDs
    ));
like image 77
Mayur Chauhan Avatar answered Oct 31 '22 11:10

Mayur Chauhan


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.

like image 20
Luke Barker Avatar answered Oct 31 '22 11:10

Luke Barker