user2151960
user2151960

Reputation: 185

Wordpress list categories with min 5 posts on it

I list my categories in a page using the following code:

        <?php
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
            ) );
        $count = count($terms);
        $categories = array();
        if ($count > 0) :
            foreach ($terms as $term) {
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                            )
                        )
                    );

                $post_from_category = new WP_Query( $args );
                if( $post_from_category->have_posts() ){
                    $post_from_category->the_post();
                }else{}
                $term->slug;
                $term->name; ?>               
                <article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
                    <a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                        <header class="entry-header">       
                            <span class="category-title"><?php echo $term->name; ?></span>
                        </header><!-- .entry-header -->
                    </a>
                </article><!-- #post-## -->
            <?php }
        endif; ?>

My question is: How can I list the categories which contains minimum 5 posts only?

Thank you very much!

Upvotes: 1

Views: 132

Answers (1)

Xhynk
Xhynk

Reputation: 13880

Inside your foreach loop you can just check to make sure $term->count is greater than or equal to 5. There's also a few other things I noticed:

  • You define a $categories array but don't appear to use it again.
  • Try not to mix if: endif; and if{} statements - stick to a syntax for ease-of-maintenance.
  • You can just check to see if $terms gets set to a truthy value instead of checking its count.
  • Are you sure your Tax Query is set properly? It's checking the taxonomy "categorys" which is a typo of "categories"
  • You don't need an empty else{} loop after your ->have_posts() check
  • You don't need to instantiate the $term object variables before the article.
  • You should consider using home_url() instead of bloginfo('url')

All that said, this should get you started:

<?php
    $term_args = array(
        'taxonomy'   => 'category',
        'hide_empty' => true,
    );

    if( $terms = get_terms( $term_args ) ){
        foreach( $terms as $term ){
            if( $term->count >= 5 ){
                $args = array(
                    'post_type'        => 'post',
                    'posts_per_page'   => 1,
                    'show_count'       => 1,
                    'orderby'          => 'rand',
                    'post_status'      => 'publish',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'categorys',
                            'field' => 'slug',
                            'terms' => $term->slug
                        )
                    )
                );

                $post_from_category = new WP_Query( $args );

                if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>               
                    <article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
                        <a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
                            <header class="entry-header">
                                <span class="category-title"><?= $term->name; ?></span>
                            </header>
                        </a>
                    </article>
                <?php }
            }
        }
    }
?>

Upvotes: 1

Related Questions