Last active
January 17, 2023 16:28
-
-
Save ankitrox/a4899063e3cf7f226f80859d4333b868 to your computer and use it in GitHub Desktop.
Count posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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