如何计算所选帖子类型的帖子总数? [英] How to count the total number of posts from the selected post types?

查看:22
本文介绍了如何计算所选帖子类型的帖子总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Wordpress 网站中制作一个通知栏,当有新帖子发布时会显示该通知栏.不幸的是,我在使代码工作时遇到了一些问题.

I want to make a notification bar in my Wordpress website, that will be displayed when there is a new post published. Unfortunately, I have some problems with getting the code work.

目标是通过通知栏让用户知道网站上有新帖子可供他们使用.所以代码应该检查帖子数量是否增加.如果是,我想在网站上显示通知栏两天.

What the goal is, is to let the user know that there are new posts available for them on the website with a notification bar. So the code should check if the amount of posts is increased. If yes, I want to show the notification bar on the website for two days.

以下代码将仅输出我在 $post_types 数组中指定的每种帖子类型的帖子总数.if 语句不能正常工作.当我发布一个新帖子,删除它并发布另一个帖子时,它不会更新数据库中的数字.只有在我删除旧的之后再发布,价值才会增加.

The following code will only outputs the total number of posts from each post type that I have specified in the $post_types array. The if statement doesn’t work correctly. When I publish a new post, delete it and post another, it will not update the number in the database. Only if I post after I delete an older one, the value will increase.

下面的代码现在只会回显帖子类型名称和帖子数量.

The code below will now only echos the post type name and number of posts.

$args = array( 
        'public' => true, 
        '_builtin' => false 
    );
    $post_types = array( 'post', 'roosters', 'downloads', 'reglements', 'alv' );

    foreach ( $post_types as $post_type ) {
        // variable
        $postCountTotal = wp_count_posts( $post_type )->publish;

        echo '<strong>' . $post_type . '</strong>';
        echo ' has total posts of : ' . $postCountTotal;
        echo '<br>';

        // First 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
        $previousCount = get_option( 'post_count_total', 0 );

        if ( $postCountTotal != $previousCount ) {
            //echo 'New post detected';
            update_option( 'post_count_total', $postCountTotal );
        } elseif ( '' == $postCountTotal && $previousCount ) {
            delete_option( 'post_count_total', $previousCount );
        }


    }
    echo $postCountTotal;

推荐答案

计算帖子以确定是否有新帖子会浪费资源并且不准确,因为帖子状态的转变会影响计数.例如,如您所说,如果一个帖子被删除并发布了一个新帖子,则计数将保持不变

Counting posts to determine if there is a new post is wasting resources and not accurate as a transition in post status can influence the count. For example, as you said, if a post is deleted and a new one is published, the count will stay the same

要完成这项工作,我们需要遵循以下工作流程

To make this work, we need to follow the following worksflow

我们需要确定帖子的发布时间.这可以通过 transition_post_status 动作钩子来完成.每次更改帖子的状态时都会触发此钩子.即使帖子更新,它的状态也会改变.

We need to determine when a post is published. This can be done via the transition_post_status action hook. This hook is fired every time a post's status is changed. Even if a post is updated, it's status is changed.

我们应该只在发布新帖子时做一些事情,所以我们需要在发布前后检查帖子的状态(这里的新"状态不像我预期的那样工作,所以我报废了那个想法).

We should only do something when a new post is published, so we need to check the post's status before and after it is published ( the 'new' status here does not work as I have expected it to work, so I scrapped that idea).

接下来是将新的帖子对象保存在 wp_options 表中,我们稍后可以在模板中获取它并使用它来显示通知栏.这里使用的函数是 add_option() 来创建我们的选项如果它不存在并且 update_option() 如果该选项已经存在.

Next will be to save the new post object in the wp_options table where we can get it later in a template and use it to display the notification bar. The functions to use here would be add_option() to create our option if it does not exist and update_option() if the option already exist.

帖子对象现在保存在 wp_options 表中.我们现在必须在函数或模板文件中检索该选项.我们将使用 get_option().从保存的帖子对象中,我们需要获取 post_date_gmt 以便将其用于 comaprison,我们需要确定 2 天后的确切时间

The post object is now saved in the wp_options table. We must now retrieve that option in an function or template file. We will use get_option(). From the post object saved we will need to get the post_date_gmt in order to use it for comaprison and we need to determine the exact time 2 days later

我们还需要当前时间,我们可以通过 current_time()

We also need the current time which we can get with current_time()

在最后阶段,我们现在可以比较日期,如果我们比较的日期少于两天,我们需要让事情发生,显示通知栏,如果比较超过两天,我们要么什么都不显示或别的东西

In the final stretch, we can now compare the dates and if the dates we are comparing is less than two days, we need to make things happen, show the notification bar, if the comparison is more than two days, we either shows nothing or something else

这是最终的代码.我已经评论好了,所以你可以关注它

Here is the final code. I have commented it well so that you can follow it

在你的functions.php中,添加以下内容

In your functions.php, add the following

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
    //Check if our post status then execute our code
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        if ( get_option( 'new_post_notification' ) !== false ) {

            // The option already exists, so we just update it.
            update_option( 'new_post_notification', $post );

        } else {

            add_option( 'new_post_notification', $post );

        }
    }

}, 10, 3 );

现在,如果您愿意,可以在您的模板或自定义函数中添加以下内容

Now, in your template or in a custom function if you wish, add the following

// Get the new_post_notification which holds the newest post
$notification = get_option( 'new_post_notification' );

if( false != $notification ) {

    //Get the post's gmt date. This can be changed to post_date
    $post_date = strtotime( $notification->post_date_gmt );

    //Get the current gmt time
    $todays_date = current_time( 'timestamp', true );

    //Set the expiry time to two days after the posts is published
    $expiry_date = strtotime('+2 day', $post_date);

    if( $expiry_date > $todays_date ) { 
        // Display your notification if two days has not been passed
    }

}

这篇关于如何计算所选帖子类型的帖子总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆