I want to compare two values, the previous amount of posts (including posts
and cpt
and when there is detected an increasing amount of posts, it should set a div element on display: block
.
I came up with the following code:
$args = array(
'public' => true,
'_builtin' => false
);
$post_types = array( 'post', 'roosters', 'downloads', 'reglements', 'alv' );
foreach ( $post_types as $post_type ) {
echo '<strong>' . $post_type . '</strong>';
$postCountTotal = wp_count_posts($post_type)->publish;
echo 'Total Posts: ' . $postCountTotal;
echo '<br>';
if ( $postCountTotal > $previous ) {
echo 'New post detected';
}
$previous = 0;
}
Above code will now print above echo’s. So it will return a list of post types
that are determined at the top of the code, and add after it the total amount of posts from that post type
.
I think I should compare the values as seen above. First you should save the previous amount of posts or reset the increaser. Then compare the value with the new one. As you can see I made an condition - if statement - to compare the previous value with the new one. Only now for every post type
it will return a echo of “New post detected”, also if there are no new posts detected.
In short, compare the previous value with the new one. If detected an increasing of the amount, set a div element display: block
for two days for example.
I look out for your response!
You need to use the database to be able to compare values from one run to another run of the script, please check in line comments
$args = array(
'public' => true,
'_builtin' => false
);
$post_types = array( 'post', 'roosters', 'downloads', 'reglements', 'alv' );
foreach ( $post_types as $post_type ) {
echo '<strong>' . $post_type . '</strong>';
$postCountTotal = wp_count_posts($post_type)->publish;
echo 'Total Posts: ' . $postCountTotal;
echo '<br>';
//first we read the previous post count value from the databse so we can compare the old value with the new one EDIT: use 0 as the default value if no data in database -first run
$previous = get_option( 'postContTotal', 0 );
if ( $postCountTotal > $previous ) {
echo 'New post detected';
//if the number of posts increased then save this new value for future compares
update_option( 'postContTotal', $postCountTotal );
}
}
extra info about WordPress Options api.