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 stack since a few hours now and I'm sure that this is not so complicated. I would like to get all custom post types into this

$posts = query_posts( $query_string.'post_type=POST-TYPE1,POST-TYPE2,POST-TYPE3&post_status=publish&posts_per_page=-1&' );

I know that there is this function: $post_types=get_post_types('','names'); And I have some Post Types which I want to exclude. E.g. "Portfolio".

So how is it possible to get the $posts looking for all important Types?!

Thanks a lot !!


share|improve this question
How do you distinguish between "important" and non(?) important posts? – kaiser Feb 9 '13 at 1:16
Well, I mean all my custom Post Types inclusive "posts" and "pages" exclusive "attachements" and "portfolio". – Philipp Feb 9 '13 at 8:52

Following code generates the array to use as argument.

// Excluded CPTs. You can expand the list.
$exclude_cpts = array(
// Builtin types needed.
$builtin = array(
// All CPTs.
$cpts = get_post_types( array(
    'public'   => true,
    '_builtin' => false
) );
// remove Excluded CPTs from All CPTs.
foreach($exclude_cpts as $exclude_cpt)
// Merge Builtin types and 'important' CPTs to resulting array to use as argument.
$post_types = array_merge($builtin, $cpts);

My suggestion is to use WP_Query instead of query_posts. See When should you use WP_Query vs query_posts() vs get_posts()?.

share|improve this answer
Thanks a lot Max! I'm so sorry but I don't get the results. Even if I try to use echo $post_types. Then I should get an list of the types, right? – Philipp Feb 9 '13 at 12:32
Sorry, there was a typo in the code. Fixed. First of all, you MUST use DEBUG when developing! That's why you don't see any errors. Second, don't echo an array, print_r() it. – Max Yudin Feb 9 '13 at 15:00
I've meant WP_DEBUG. – Max Yudin Feb 9 '13 at 15:10
Thank you, Max! The Debug-Mode didn't help because I've not been able to load anything. ;) But now I get an result: Array ( [0] => post [1] => page [n-finalist] => n-finalist [event] => event [ancient] => ancient [w-finalist] => w-finalist [c-pages] => c-pages [n-page] => n-page [w-page] => w-page [worldtour] => worldtour [about] => about ) Array That's great. But now I need a comma separated list. I didn't find something about how to get just everything from [xxx] in print_r; – Philipp Feb 10 '13 at 10:34
See PHP docs for explode(). – Max Yudin Feb 10 '13 at 11:48

Basically it's a wp_filter_object_list() call specifying names as output and and as operator. As you can't distinguish between portfolio and the rest by it's objects data, you'll have to do unset that one manually.

$cpts = get_post_types( array(
    'show_in_nav_menus' => false
) );
unset( $cpts['portfolio'] );
foreach ( $cpts as $cpt )
    $r[] = get_post_type_object( $cpt );

     '<pre>%s</pre><br /><pre>%s</pre>'
     // The names only
    ,var_export( $cpts, true )
     // The full post type objects
    ,var_export( $r, true )
share|improve this answer

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.