WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query categories when it has at least 3 posts (6 posts)

  1. Beee
    Member
    Posted 3 years ago #

    I'm now listing the last added categories as such

    $args = array(
    	'exclude' => array(1,3,4),
    	'show_count' => 1,
    	'orderby' => 'ID',
    	'order' => 'DESC',
    	'number' => '10',
    	'title_li' => ''
    ); ?>
    wp_list_categories( $args );

    I want to query the category only if it has 3 posts or more...

    if anyone knows how to query that, plz post it

  2. Coen Jacobs
    Member
    Posted 3 years ago #

    I think the only way to achieve this is to use get_categories() and then loop trough the list manually and kick out every category that has less than 3 posts.

    As far as I know, there is no WordPress function that provides an argument that lets you select a minimum number of posts.

  3. Beee
    Member
    Posted 3 years ago #

    I was affraid already there was nothing that could be achieved with 'regular' code....

    I thought the prob with get_categories() is there's no post count (afaik)
    I do would like to show that... but then I found a term on the forlast line "$category->count" so will try to play with that...

  4. Coen Jacobs
    Member
    Posted 3 years ago #

    Yeah, count should do the trick. Filter out each category that has a value less then 3 in $category->count. :)

  5. Beee
    Member
    Posted 3 years ago #

    for those who want to know how it works...

    $args = array(
    	'exclude' => array(1,3,4),
    	'show_count' => 1,
    	'orderby' => 'ID',
    	'order' => 'DESC',
    	'title_li' => ''
    );
    $categories = get_categories( $args );
    $counter = 0;
    foreach($categories as $category)
    {
    	if ( $category->count > 2 )
    	{
    		$counter++;
    		if ( $counter < 11 )
    		{
    			echo '<li class="counter-' . $counter . '"><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all galleries for %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ('. $category->count . ')</li>';
    		} continue;
    	}
    }
  6. eleven.mary
    Member
    Posted 3 years ago #

    Thank you, it was very usefull for me.

Topic Closed

This topic has been closed to new replies.

About this Topic