Title: weird problem &#8211; can&#039;t query custom posts
Last modified: August 21, 2016

---

# weird problem – can't query custom posts

 *  Resolved [Max](https://wordpress.org/support/users/panmac/)
 * (@panmac)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/)
 * Hi there –
 * I’ve used this plugin for ages with no problem, and now for whatever reason I’m
   working on a theme with it and I can’t query the custom post types.
 * First, when I was querying the CPT (in this case, ‘proyectos’), I was wrapping
   each proyecto post in a div, it the query was just outputting 10 empty divs.
 * I thought it might be the db, so I moved my theme files to a clean install of
   WP with a clean db.
 * Installed the plugin fresh, created the post type Proyecto and left all default
   settings except making it hierarchical, published a post. I can view the post
   no problem : [http://gentry1.bluestormcreative.com/proyecto/project-1/](http://gentry1.bluestormcreative.com/proyecto/project-1/)
 * Now I’ve got my page template set up with a loop to output this custom post type,
   and it is only pulling all the OTHER posts. (in this case, created by the wp 
   example content plugin): [http://gentry1.bluestormcreative.com/proyectos/](http://gentry1.bluestormcreative.com/proyectos/)
 * Here is my template code:
 *     ```
       <?php get_header(); the_post(); ?>
   
       	<section id="primary" class="content-area">
       		<main id="main" class="site-main" role="main">
       			<h1><?php the_title(); ?></h1>
   
       	<?php
       	        global $wp_query;
       	        query_posts(array(
       	            'post_type' => 'proyecto'
       	        ));
       	        while(have_posts()) : the_post(); ?>
   
       	            <li <?php post_class(); ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       	            <?php
       	        endwhile;
       	        wp_reset_query();
       	        ?>
   
       		</main><!-- #main -->
       	</section><!-- #primary -->
   
       <?php get_sidebar(); ?>
       <?php get_footer(); ?>
       ```
   
 * I’ve also tried a WP_Query loop with:
    `$query = New WP_Query('post_type=proyecto');`
 * but that does the same thing – no posts.
 * What the heck am I missing? I’m starting to feel a little crazy. I’ve tested 
   with the default theme and this custom template, with all other plugins disabled,
   with wp_debug on, and also by copying the code from the plugin and putting it
   in my function file and then turning the plugin off – nothing works so far.
 * Any ideas would be SO greatly appreciated!! Thank you in advance!!
 * [http://wordpress.org/plugins/custom-post-type-ui/](http://wordpress.org/plugins/custom-post-type-ui/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302787)
 * That’s a bit odd.
 * Couple things, just to be certain. The post type records are still in your database
   right? Unregistering or not having a post type registered won’t alter the db.
 * Could you check/paste the output of this:
 *     ```
       $posttype = get_post_type_object( 'proyectos' );
       print_r( $posttype );
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_post_type_object](http://codex.wordpress.org/Function_Reference/get_post_type_object)
 * I’m not seeing anything wrong with your attempts to display the post types, so
   I’m wondering if it’s something going on before that. The only thing I could 
   see as maybe the issue is your attempt to loop with the new WP_Query version.
   You’re using $query->have_posts() and $query->the_post() correct?
 *  Thread Starter [Max](https://wordpress.org/support/users/panmac/)
 * (@panmac)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302841)
 * Hi Michael – Thanks for your response. There’s something weird going on – printing
   out the $posttype returned the object as you would expect – maybe there’s a setting
   in there that I’m checking off wrongly? I wouldn’t think that would make the 
   post un-loopable. I left the output on the page for reference. [http://gentry1.bluestormcreative.com/proyectos/](http://gentry1.bluestormcreative.com/proyectos/)
 * I also tried again to switch the query to a WP_Query just to double check, and
   again the output is all the other posts:
 *     ```
       <?php
       	$query = New WP_Query('post_type=proyecto');
       	if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
       <li <?php post_class(); ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
   
       	<?php endwhile; ?>
       		<!-- post navigation -->
       	<?php else: ?>
       		<!-- no posts found -->
       <?php endif; wp_reset_query(); ?>
       ```
   
 * I’m at a loss with this one. Maybe going to start from scratch a second time 
   and write the code without the plugin?
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302842)
 * We’re not doing anything special that you can’t do with register_post_type. Any
   setting we provide should correspond with one of the arguments for that function.
 * If you could show me the print_r results of the following, that’d be awesome.
 *     ```
       <?php
       $args = array(
           'post_type' => 'proyectos',
           'posts_per_page' => 5,
           'post_status' => 'publish'
       );
       $ourquery = new WP_Query( $args );
   
       print_r($ourquery); ?>
       ```
   
 * It’s definitely getting registered. Just out of forehead slapping potential, 
   do you have any posts in this post type yet?
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302843)
 * as another possible issue, are you using pre_get_posts anywhere in your theme
   or a plugin? that has the potential to screw up queries as well. There isn’t 
   anything that would make this “unloopable” it’s a question of if we’re managing
   to get any results to loop.
 *  Thread Starter [Max](https://wordpress.org/support/users/panmac/)
 * (@panmac)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302886)
 * Hi Michael –
 * THANK YOU for the pre_get_posts suggestion – that was exactly the problem. I 
   forgot that I had been working on adding posts to category archives and had left
   some hanging code there, and when I was testing the dev server connection was
   bad, so when I was commenting out that code it wasn’t registering.
 * For anyone else with this problem, I fixed it by modifying the pre_get_posts 
   function I was using from this:
 *     ```
       function custom_post_archive($query) {
           if (!is_admin() && ($query->is_archive) ) {
               $query->set( 'post_type', array('productos', 'proyectos', 'nav_menu_item', 'post') ); //preserve nav menu on templates
           remove_action( 'pre_get_posts', 'custom_post_archive' );
       	}
       }
       add_action('pre_get_posts', 'custom_post_archive');
       ```
   
 * to this:
 *     ```
       function dvo_add_custom_types( $query ) {
           if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
               $post_types = get_post_types( '', 'names' );
               $query->set( 'post_type', $post_types);
               return $query;
           }
       }
       add_filter( 'pre_get_posts', 'dvo_add_custom_types');
       ```
   
 * Which I found in the comments here: [http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/](http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/)
 * And that allowed my category archives AND preserved the proper loops.
 * Thanks again for your support – really great, for a really great plugin.
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302891)
 * panmac, just as an added precaution, one that would help as you’ve already been
   bitten by a similar case. I’d check that you’re altering just the main query.
   Example:
 *     ```
       if ( $query->is_main_query() ) {
           //Do something that you want to occur on just this.
       }
       ```
   
 * You’ll find that WP_Query is used on many things along the process of loading
   a page. I’d also suggest keeping “!is_admin()” because this hook runs in the 
   admin as well

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘weird problem – can't query custom posts’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 6 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/weird-problem-cant-query-custom-posts/#post-4302891)
 * Status: resolved