• I’ve placed a query post above my loop with “the_content” looping. The problem I’m having is that when I add the query, I no longer am able to see the content that I typed in the admin panel.

    <?php query_posts ( 'cat=7&posts_per_page=-1' ); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    As soon as I delete query_posts, my content shows up. What am I missing?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Do you get the ‘Sorry, no posts . . .’ error, or nothing at all? If you get the error message, maybe your cat id is not correct.

    Thread Starter get_username

    (@get_username)

    No, nothing shows up other than the post when I have the query in place. When I delete the query, anything that I had typed in the content form will appear on the page.

    nothing shows up other than the post when I have the query in place

    Other than the post? What post?

    Try creating a new query object:

    <?php $myquery = new WP_Query ( 'cat=7&posts_per_page=-1' ); ?>
    
    <?php if ( $myquery->have_posts() ) : while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    Thread Starter get_username

    (@get_username)

    @esmi: I have posts that I’m filtering by category to display on a page. When I have query_posts above my loop, it displays the category I stated in the query (7 in this case), but not the text from (the_content) which would appear from typing within the admin page panel. Make sense?

    @vtxyzzy: Thanks, I’ll give new WP_Query a try this evening and report back.

    Don’t bother – I answered the wrong question. I thought you had a second loop below the one you showed.

    So, just to be sure I’m on the right track: You only have one loop but you want the content from the current page to show above the category loop?

    If that is the case, you wil need 2 loops – one for the content of the current page and another for the category posts:

    <?php
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();  // Display content of current page
    endwhile;
    endif;
    
    // Now get the category posts
    $myquery = new WP_Query ( 'cat=7&posts_per_page=-1' ); ?>
    
    <?php if ( $myquery->have_posts() ) : while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    By the way, I think you might have trouble with pagination if you have more category posts than you want to show on one page. The loop for the current page will probably intercept the pagination. I can’t be sure, but I think this is the case.

    Thread Starter get_username

    (@get_username)

    Thanks, vtxyzzy. Just for the heck of it, I tried using:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php the_content(); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>	
    
    <?php wp_reset_query(); ?>
    
    <?php query_posts ( 'cat=7&posts_per_page=-1' ); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title (); ?></h2>
    <?php the_content(); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>	
    
    <?php wp_reset_query(); ?>

    which is basically the post configured the same way twice, but with the wp_reset_query. It does work, but I wonder if this is not standard practice, and if your suggestion is better form?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query posts hiding my content’ is closed to new replies.