0

I would like use two loops in the results after a search.

First of all, I begin the loop if there is result

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

and after a loop to display the articles belonging a category

<?php $cats = get_categories();

    foreach ($cats as $cat) {
        query_posts('cat='.$cat->cat_ID); ?>

        <h2><?php echo $cat->cat_name; ?></h2>

        <ul>
            <?php while (have_posts()) : the_post(); ?>
            <li><?php the_title(); ?></li>
            <?php endwhile;  ?>
        </ul>

        <?php } ?>

But it seems it display all the articles or make a infinite loop and doesn't fit with request made at the begining.

EDIT : In this result page, I would to organize by category. For instance, if I have four articles for the same category, it display just once the category name.

Category_name_1

  • First article

  • Second article

  • Third article

Category_name_2

  • First article
2
  • This question is very confusing. The latest edit more or less reverses what the original question appeared to be asking. Please do a better job with the explanation. I am feeling like I have been wasting my time. – s_ha_dum Nov 17 '13 at 18:06
  • You're right, it's isn't clear, but I didn't succeed to explain easily. I will write the all post soon. – jeremybarbet Nov 17 '13 at 22:08
0

Add wp_reset_postdata(); after each loop, if you use your custom query, you should use wp_reset_query();

1
  • wp_reset_postdata() doesn't work whereas wp_reset_query() work and avoid the infinite loop. But it duplicate twice the content and doesn't fit to the request. – jeremybarbet Nov 16 '13 at 20:52
0

Your outer Loop uses the data in the global variable $wp_query. query_posts changes the data in the global variable $wp_query. Since you run query_posts at each iteration of the Loop, clobbering $wp_query every time, an infinite Loop is pretty much inevitable, along with other unwanted behavior. It is like trying to count to 100 but every time you get to a number you have to start over with some other random number. It doesn't work.

The solution is don't clobber $wp_query which you would do by following the rule that ought to be this site's motto: Don't use query_posts, ever.

Instead, create a new WP_Query object as in this example from the Codex:

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

In your case, I am not 100% sure what you are aiming for but to show posts from all categories, this should do it:

$cats = get_categories();

foreach ($cats as $cat) {
  $catposts = new WP_Query(
    array(
      'cat' => $cat->cat_ID,
      'ignore_sticky_posts' => true,
      'posts_per_page' => -1
    )
  ); ?>

  <h2><?php echo $cat->cat_name; ?></h2>

  <ul><?php 
    while ($catposts->have_posts()) {
      $catposts->the_post(); ?>
      <li><?php the_title(); ?></li><?php 
    } ?>
  </ul><?php
}

Now, I think you might want to only show links to other posts in the same category as the current post in the outer Loop. For that you do not want get_categories, you want wp_get_post_categories

if (have_posts()) { 
  while (have_posts()) {
    the_post();
    $cats = wp_get_post_categories($post->ID,array('fields' => 'all'));

    foreach ($cats as $cat) {
      $catposts = new WP_Query(
        array(
          'cat' => $cat->term_id,
          'ignore_sticky_posts' => true,
          'posts_per_page' => -1
        )
      ); ?>

      <h2><?php echo $cat->name; ?></h2>

      <ul><?php 
        while ($catposts->have_posts()) {
          $catposts->the_post(); ?>
          <li><?php the_title(); ?></li><?php 
        } ?>
      </ul><?php
    }
  }
}
9
  • It's in the if part where category will appear, and in the while part where the differents articles of this category will appear ? It's in the $args I need to call category ? Sorry I don't understand everything. – jeremybarbet Nov 16 '13 at 21:39
  • Yes, in the $args you will need to specify your category. – s_ha_dum Nov 17 '13 at 4:38
  • But when I do : $args = array('cat' => 'cat_ID'); it returns all the articles and for each articles, the category. If I have three articles from the same category, I would to print only one time the category name. – jeremybarbet Nov 17 '13 at 9:49
  • Your syntax is wrong. The category ID isn't cat_ID, it is $cat->cat_ID as you used in your own code. Why did you not do the same thing? – s_ha_dum Nov 17 '13 at 14:35
  • I will be honest, I think I didn't understand everything right now. I need to keep the <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> and replace the second loop with the WP_Query ? – jeremybarbet Nov 17 '13 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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