query_posts() ALWAYS displays something?

OK, I’m using posts_query() to display posts.

The problem is, at least in my case, posts_query() always outputs something.

Read More

For example:

<?php

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("posts_per_page=1&paged=$paged");
    global $more;
    $more = 0;

    while ( have_posts() ) : the_post(); ?>    
        <!--- DO NOTHING ! -->
    <?php endwhile ?>

Displays raw first part of a post (and without $more = 0 the whole post).

Why is it happening?

I want to style the output on my own, but I’m not able to, because for example:

while ( have_posts() ) : the_post(); ?>    
         <h1><?php the_title();?></h1>
         <h3><?php the_content( __( '') ); ?></h3>
<?php endwhile ?>

Gives:

<h1>MyTitle</h1>
<h3>something something something something something</h3>
<p>something something something something something</p> <!-- (wherethis line comes from? ;/) ?>

PS.

In addition I’m almost sure pagination doesn’t work as well, and I believe it should this way? I have around 15 posts to display, I’ve chosen 1, why there’s no navi? 🙁

The exact code:

function posts_shortcode( $atts ) {
     extract( shortcode_atts( array(
        ), $atts ) );  

        query_posts("posts_per_page=1");
        global $more;
        $more = 0;

        while ( have_posts() ) : the_post(); ?>    
         <h1><?php the_title();?></h1>
         <h3><?php the_content( __( '') ); ?></h3>
         <?php endwhile;
}

add_shortcode('posts', 'posts_shortcode');  

Related posts

Leave a Reply

2 comments

  1. query_posts() ALWAYS displays something?

    No it doesn’t at least not for me, i’ve tried the code you posted inside my child theme and was unable to reproduce the issue described.

    Firstly, i tried…

    while ( have_posts() ) : the_post(); ?>    
        <!--- DO NOTHING ! -->
    <?php endwhile ?>
    

    ..and got nothing, so i then tested..

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("posts_per_page=1&paged=$paged");
    global $more;
    $more = 0;
    
    while ( have_posts() ) : the_post(); ?>    
             <h1><?php the_title();?></h1>
             <h3><?php the_content( __( '') ); ?></h3>
    <?php endwhile ?>
    

    ..which produced..

    <h1>Post title</h1>
    <h3><p>post content</p></h3>
    

    No stray paragraph of content.

    If i had to guess at the problem, i’d say there’s a badly coded filter or shortcode at work. Easiest way to isolate the cause (as with any WP troubleshooting) would be to disable plugins and/or switch theme and narrow down which is causing the problem.

    UPDATE:
    Use a new WP_Query object instead of query_posts and that should clear up the problem.

    function posts_shortcode( $atts ) {
        extract( shortcode_atts( array(
        ), $atts ) );  
        global $more, $wp_query;
        $args = array( 
          'posts_per_page' => 1,
          'paged' => get_query_var('paged')
        );
        $q = new WP_Query;
        $q->query( $args );
    
        // Backup $wp_query
        $backup = $wp_query;
        // Fill $wp_query with the custom query
        $wp_query = $q;
    
        // Do the loop
        while ( $q->have_posts() ) : 
          $q->the_post(); 
          $more = 0;
        ?>    
    
        <h1><?php the_title();?></h1>
        <h3><?php the_content( __( '') ); ?></h3>
    
        <?php 
        endwhile;
    
        // Output page navi
        wp_pagenavi();
    
        // Restore $wp_query
        $wp_query = $backup;
    
        // Restores wp_query global and also resets postdata(may not be needed, but won't hurt)
        wp_reset_query(); 
    }
    
    add_shortcode('posts', 'posts_shortcode'); 
    
  2. Does same thing happen if you use the_post() without while loop? Does same thing happen if you don’t use custom query_posts() call?

    This seem awfully like poorly coded filter somewhere in chain, but it’s hard to guess location from this.

    You could try this code to dump names of filters as they run and try to pinpoint which might cause it:

    add_filter('all','dump_name',10,0);
    
    function dump_name() {
    
        var_dump(current_filter());
    }