Skip to content

Instantly share code, notes, and snippets.

@ankitrox
Last active January 17, 2023 16:28
Show Gist options
  • Save ankitrox/a4899063e3cf7f226f80859d4333b868 to your computer and use it in GitHub Desktop.
Save ankitrox/a4899063e3cf7f226f80859d4333b868 to your computer and use it in GitHub Desktop.
Count posts
<?php
function count_posts_single_type( $type = '' ) {
$count = rand( 1, 10 );
echo 'Count for post type ' . $type . ' is ' . $count;
echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
return $count;
}
function count_posts_multiple_types( $types = [] ) {
$numposts = 0;
foreach( $types as $type ) {
$numposts += count_posts_single_type( $type );
}
return $numposts;
}
function count_posts( $post_type = [] ) {
if ( is_array( $post_type ) ) {
return count_posts_multiple_types( $post_type );
}
return count_posts_single_type( $post_type );
}
$post_types = [ 'post', 'page', 'recipe' ];
$a = count_posts( $post_types );
echo 'Total count posts for a is ' . $a;
echo PHP_EOL;
echo PHP_EOL;
$p_types = [ 'product', 'magazine', 'leads' ];
$b = count_posts( $p_types );
echo 'Total count posts for b is ' . $b;
echo PHP_EOL;
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment