0

I'm trying to select all rows from the database regardless of post type, however when suppressing post_type WP_Query only gets rows with post_type = "post" is there a way to suppress the post_type filter (or select all post types)?

// This just selects post_type = "posts"
$args = array(
 'post_status' => 'publish',
 'posts_per_page' => -1,
 'ignore_sticky_posts' => true,
);
$qry = new WP_Query( $args );
0

By default-- that is, unlss told otherwise-- WP_Query will search the post post type. You may supply 'post_type' => 'any' to retrieve all post types, but be aware that things like attachments are post types. For example...

$args = array(
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'ignore_sticky_posts' => true,
  'post_type' => 'any'
);
$qry = new WP_Query( $args );
var_dump($qry->request);

Post types set as not searchable, such as the menu post type (which has no business being a post type at all IMHO), will be ignored. If you need those odd-ball post types, you will need to list them. get_post_types() should help with that.

$args = array(
   'post_status' => 'publish',
   'posts_per_page' => -1,
   'ignore_sticky_posts' => true,
   'post_type' => get_post_types(),
);
$qry = new WP_Query( $args );
| improve this answer | |
  • I've been making some tests and it seems that this is not the same as SELECT * FROM wp_posts WHERE post_status = 'publish'. Some post types like nav_menu_item are not being selected. Any ideas how to include them? – Lisandro Vaccaro Mar 10 '14 at 17:23
  • @LisandroVaccaro : see the edit. – s_ha_dum Mar 10 '14 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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