WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts & WP_Query not working in categories (4 posts)

  1. Valdi
    Member
    Posted 4 years ago #

    Hi,

    I'm having a sudden problem with some custom loops, not sure what triggered it because it used to work fine before.

    I have 4 loops:
    1=header
    1=sidebar
    1=page
    1=footer

    The problem is that in one page template and categories all the custom loops will show ALL the posts even though I've chosen to show only from few categories.
    Here's the code for Custom loop 1:

    <?php if (have_posts()) : ?>
     <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     query_posts("cat=9,29,7,27,8,28&paged=$paged&posts_per_page=15&orderby=rand"); ?>
     <?php while (have_posts()) : the_post(); ?>
    
    <--- Here I have the content --->
    
    <?php endwhile; ?>
    <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    <?php endif;?>

    ^
    This gives me 5 posts per page from ALL categories.

    When I'm on a category page it will cause the same problem to all the rest of my loops (header,sidebar,footer)

    What have I done wrong? Like I said, this used to work fine before but somehow I've managed to break it.

    I'd appreciate any help :)

  2. Valdi
    Member
    Posted 4 years ago #

    I've managed to make one of the loops work in a page template using this:

    <?php
    $my_query = new WP_Query('cat=9,29,7,27,8,28&paged=$paged&posts_per_page=15&orderby=rand');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    <--- Content here --->
      <?php endwhile; ?>
    <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
      <?php endwhile; ?>
    <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    <?php endif;?>

    But the pagination won't work now...

    This is the loop for categories:

    <?php if (have_posts()) : ?>
                   <?php while (have_posts()) : the_post(); ?>
    <--- Content here --->
    <?php endwhile; ?>
    <?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
    <?php endif; ?>

    ^ When I go to a category this will list posts from all categories...

  3. Valdi
    Member
    Posted 4 years ago #

    Tested this on a previous version of WP and everything seems to work fine.

    So I guess it has to do with the 2.9 version...

    Any help would be appreciated.

    Thanks.

  4. Valdi
    Member
    Posted 4 years ago #

    Found the problem... the plugin WPML Multilingual CMS causes the problems.

Topic Closed

This topic has been closed to new replies.

About this Topic