Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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) :
        print_r($post);             
        endforeach;
    endif;
endforeach;

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

2 Answers 2

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

 
discard

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.