Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

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');  
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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'); 
share|improve this answer
    
@t31os, OK, so I'm going to investigate my theme then, the query_posts() code above is a part of shortcode in functions.php file. There are no other loops around and that's the problem. edit turning off all widgets and plugins didn't help, still searching. [edit2] Deleting all other shortcodes from functions.php also doesn't change a thing. –  Wordpressor Mar 6 '11 at 11:19
1  
@Wordpressor - Ya know, actually it should work if you're using it on a non-paged page, ie. a page with content that isn't span across pages. Adding 'paged' => get_query_var('paged') inside the args array works for me when testing the shortcode in a page. –  t31os Mar 7 '11 at 16:40
1  
Have added in, though it's exactly what i posted in my last comment. If you're having problems, could tell me where you're adding the shortcode(inside a page/post/custom type). –  t31os Mar 7 '11 at 23:39
1  
Ah, so it wasn't a problem of paging not working, just the paging links not appearing, right? You can always test manually by just adding the paged query var onto the url, eg. example.com/some-page-name/page/2. Off to bed shortly, but i'll come up with something to fix the issue for you tomorrow(my immediate thoughts would be to do the paging links inside the shortcode, and use a little variable juggling to trick pagination into working - but i'll need to think about it tomorrow). –  t31os Mar 7 '11 at 23:57
1  
Updated answer, just needed to trick page navi by filling up $wp_query temporarily and restoring it after printing the paging links. Hope that helps. –  t31os Mar 8 '11 at 9:38

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());
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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