0

The error I'm seeing is that the way I currently have my category page set up, if a post on that page has more than 1 category, the loop won't load the posts that follow the one with multiple categories.

Currently I have about 10 posts in my test installation of WordPress. 9 of them have the same category, lets call it "Surprised". 1 of them has 3 categories. When I go to the category for page "Surprised, instead of seeing 10 posts on that page, including the one with 3 different categories, the page stops loading the category posts that follow it.

Here is an example of one of the loops that ate on the category page (there are 4 loops total and all of them behave the same presently).

<? get_header(); ?>

<?
$cat_array = get_the_category();
$cat_name = esc_html($cat_array[0]->name);
?>
    <?
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args  = array(
      'category_name'  => $cat_name,
      'paged'          => $paged,
      'posts_per_page' => 2
    );
    $wp_query = new WP_Query($args);
    while($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="post-box pane">
      <a href="<? the_permalink(); ?>" class="article">
        <h3 class="title"><? echo get_the_title(); ?></h3>
        <? echo the_post_thumbnail('smaller-general-thumb', array('class' => 'thumb')) ?>
      </a>
    </div>
    <? endwhile; wp_reset_postdata(); ?>

The reason I'm doing 4 sections with different loops is because each section has a specific number of posts in it. I have to limit the number of posts that show for a section, so I create a new loop and I limit the posts for that loop by paging them and limiting posts per page.

  • Category archives do not need to get_the_category() again. Simply do a simple WP Loop (the loop), here you can see: github.com/Automattic/_s/blob/master/archive.php – Mayeenul Islam Nov 29 '15 at 9:36
  • what if I have 4 loops on that page that need to be offset? Thanks for your answer by the way. – ninja08 Nov 29 '15 at 9:39
  • You are doing it all wrong if you have 4 loops on your category archive page. You are going to have an endless amount of crap, gaurenteed ;-) – Pieter Goosen Nov 29 '15 at 10:00
  • Unless you're offering me a solution, you're response doesn't really help me much @PieterGoosen – ninja08 Nov 29 '15 at 10:03
  • 1
    if you are running multiple loops to divide the page into sections, see this answer for splitting a single query among multiple sections. – Milo Nov 29 '15 at 16:52
1

Multiple Loops

Outside the loop you can get available terms of the core taxonomy category with get_terms( 'taxonomy_name' ). The resulting array contains objects like

object(stdClass)#141 (9) {
    ["term_id"] => string(1) "3"
    ["name"] => string(9) "The Name of your Category"
    ["slug"] => string(9) "name-of-tax-term"
    ["term_group"] => string(1) "0"
    ["term_taxonomy_id"] => string(1) "3"
    ["taxonomy"] => string(11) "slug_of_tax"
    ["description"] => string(41) "Description of Term."
    ["parent"] => string(1) "0"
    ["count"] => string(1) "3"
}

So you would want to get the category slug, not its name:

$cats = get_terms( 'category' );

You could then loop the categories like

foreach ( $cats as $cat ) {
    $args  = array(
      'category_name'  => $cat->slug,
      'paged'          => $paged,
      'posts_per_page' => 2
    );
    // the loop
    wp_reset_postdata();
}

One Loop

If your loop runs on the unchanged main query, the current category is already present in the query.

Just change

$args  = array(
  'category_name'  => $cat_name,
  'paged'          => $paged,
  'posts_per_page' => 2
);

to

$args  = array(
  'paged'          => $paged,
  'posts_per_page' => 2
);

in your code.

Besides, when not using get_the_category() inside the loop, you have to pass a post ID as argument.

| improve this answer | |
  • I think I should be using the unchanged main query, but now, the category pages seem to run their loops like the index loop... just listing the posts from more recent to older. – ninja08 Nov 29 '15 at 9:49
  • updated my answer, hope that helps. – Hans Spieß Nov 29 '15 at 10:06
-1

This is the solution I found to be the most elegant and appropriate for my situation:

<? global $wp_query; ?>

$args  = array_merge( $wp_query->query_vars, array( 'posts_per_page' => 2));
$the_query = new WP_Query($args);
while($the_query->have_posts()) : $the_query->the_post(); ?>

/// The loop

<? endwhile; rewind_posts(); ?> 

I specifically decided to use array_merge in order to merge the global query with the loop I'm writing in order to limit the number of posts for that loop. Now I don't need to specify the category since that's already in the global query. No need to update dat bish myself.

Thank you everyone for your help!

| improve this answer | |
  • negative 1 for what reason? Don't down vote without proper feedback. At least in the future I can correct myself. – ninja08 Nov 30 '15 at 3:48
  • 1
    This is an even worst solution than your actual problem. @Milo has linked to a post which will solve your issue, no idea why you do not want to use it. Using query_posts is really really bad, and running it 5 times is 5 times worst. If you are after SEO and page rankings, you will never reach top 1000 as query_posts slows your page down dramatically, and using it 5 times adds up to even slower page loads. Consider using the solution that @Milo have given you – Pieter Goosen Nov 30 '15 at 4:37
  • Thank you for sharing. I didn't know that. If it works I'm more than happy to give him credit. – ninja08 Nov 30 '15 at 6:05
  • I updated my answer with a more performant version of what I had. – ninja08 Nov 30 '15 at 6:56
  • Why are you still running custom queries. It still makes no sense running custom queries. Your question also still does not really explain why – Pieter Goosen Nov 30 '15 at 6:59

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.