Multiple WordPress custom queries behaving oddly

First off, here’s the code :

    <?php
    $categories = get_categories();

    foreach ($categories as $cat) { if ($cat->count >= 4) { ?>
    <section class="home-section row">
        <div class="large-12 columns">
            <h3 class="ug-home-title"><span><?=$cat->name;?></span></h3>
            <div class="row">
                <div class="large-6 columns">
                    <?php
                        $args = array(
                            'post_type' => 'post',
                            'category_name' => $cat->name,
                            'posts_per_page' => 1
                        );
                        $query = new WP_Query($args);
                        while ($query->have_posts()) : $query->the_post();
                    ?>
                    <article class="ug-panel">
                        <ul class="ug-tag-list">
                            <?php
                                $post_categories = wp_get_post_categories( $post->ID );
                                foreach ($post_categories as $c) {
                                    echo '<li><a href="'.get_category_link( get_cat_ID(get_category( $c )->name) ).'">'.get_category( $c )->name.'</a></li>';
                                }
                            ?>
                        </ul>

                        <?php (has_post_thumbnail()) ? the_post_thumbnail() : displayBackupImage(); ?>

                        <footer class="ug-panel-inner">
                            <p><em><?= 'il y a '.human_time_diff( get_the_time('U'), current_time('timestamp') ); ?></em></p>
                            <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
                        </footer>
                    </article>
                    <?php endwhile; wp_reset_postdata()?>
                </div>
                <div class="large-6 columns">
                    <ul class="ug-article-list">
                        <?php
                            $args = array(
                                'post_type' => 'post',
                                'category_name' => $cat->name,
                                'posts_per_page' => 3,
                                'offset' => 1
                            );
                            $query = new WP_Query($args);
                            while ($query->have_posts()) : $query->the_post();
                        ?>
                        <li>
                            <article class="clearfix">
                                <a href="<?php echo get_permalink(); ?>" class="left picture">
                                    <?php (has_post_thumbnail()) ? the_post_thumbnail() : displayBackupImage(); ?>
                                </a>
                                <h5><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> <br><small>publié il y a <em><?= human_time_diff( get_the_time('U'), current_time('timestamp') ); ?></em></small></h5>
                            </article>
                        </li>
                        <?php endwhile; wp_reset_postdata();?>
                    </ul>
                </div>
            </div>
        </div>
    </section>
    <hr class="home-hr">            
    <?php }} ?>

As you can see, there is first a simple foreach loop for the categories containing more than 4 posts. Then, I am using two different custom queries to display those posts in two different places in my code.

Read More

This works perfectly, but for some reason, it will invariably fail on some categories despite them containing posts, and I have no idea why. I’ve attached a screenshot so you can see the output.

Any answer is greatly appreciated.

EDIT: it would seem to happen when a post belongs to two categories at the same time… A conflict maybe? I really am at a loss here.

EDIT 2: nope, it has nothing to do with that, some categories display the same post twice just fine…

The screenshot

Related posts

Leave a Reply

1 comment

  1. It turns out I’m a moron.

    The answer lies in changing this 'category_name' => $cat->name to this 'category_name' => $cat->slug

    The slugs are the real identifiers. The name property can be “Banana” and it’ll still link to the actual category.