WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a plugin in which I need to list post/page/attachment by each type.

First I get all post type:

$post_types = get_post_types (array('public'=>true));

Then I create a loop and query posts for each post type, and print out:

foreach ($post_types as $type_name => $post_type) : 
    $args = array(
        'post_type' => $post_type,
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => null, // any parent
    $posts = get_pages($args);      
    if ($posts):            
            foreach ($posts as $post) :

However, on plugin page, I don't see anything print out.

Could anyone please give a hint?

Thanks a lot!

share|improve this question
get_pages() is deprecated... use get_posts() or new WP_Query(). – kaiser Jun 22 '13 at 10:21

Instead of using get_pages use

new WP_Query($args)

It will get all the posts/pages/attachments

share|improve this answer
Hi Kuljeet, WP_Query works for me. Thanks alot. I am sorry that I don't have enough reputation to vote your answer :( – Dau Hai Jun 22 '13 at 16:01

Use $post_types = query_posts('post_type'=>'post/page'); for specific post types.

share|improve this answer
query_posts() should never be used, and your code cannot work. Also, an explanation would be useful. – toscho Jun 22 '13 at 13:12
I have found query_posts() more useful and faster then WP_Query();. I don't know why you are telling it shouldn't be used. If i use like example of "Portfolio" type custom posts. Then it should be like this $post_type= query_posts(array('post_type' => 'portfolio', 'posts_per_page' => -1)); – h2cweb.net Jun 22 '13 at 13:17
It ruins the main query and pagination, it cannot be faster than WP_Query, because it is a wrapper for that. – toscho Jun 22 '13 at 13:21
Forgot to say in the end of query it needs to reset the query. wp_reset_query(); Please see the reference for better understand. codex.wordpress.org/Function_Reference/query_posts – h2cweb.net Jun 22 '13 at 13:24
I know that function in and out. Plugins hooking in during an active query_posts() get still the wrong information. Always use WP_Query or a filter on pre_get_posts. – toscho Jun 22 '13 at 13:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.