• Resolved Max

    (@panmac)


    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 : https://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): https://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!!

    https://www.ads-software.com/plugins/custom-post-type-ui/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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 );

    https://codex.www.ads-software.com/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

    (@panmac)

    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. https://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

    (@tw2113)

    The BenchPresser

    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

    (@tw2113)

    The BenchPresser

    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

    (@panmac)

    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: https://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

    (@tw2113)

    The BenchPresser

    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.