如何统计一个CPT的所有帖子?

问题描述 投票:0回答:1

我的 wordpress 房地产主题有一个自定义的属性帖子类型。

后台状态:全部、我的、已发布、草稿、已被用户禁用

我有一个自定义字段(prop),我试图在发布新帖子时自动完成它。 Wordpress ID 计数不是按 1 递增。所以我想使用该字段自动完成它(帖子数 + 1)

我有这个功能

/**   Generate Custom ID when a new Estate Property is published.*/
add_action('publish_estate_property','save_post_callback');
function save_post_callback($post_id){
    global $post; 
    $no_of_posts = wp_count_posts("estate_property");
    if(get_post_meta($post_id, "prop", true) == "" && !wp_is_post_revision($post_id))
            
        $_POST["prop"] = ($no_of_posts->publish + $no_of_posts->draft + 1);
}

我试过了

$_POST["prop"] = ($no_of_posts->publish + $no_of_posts->draft + 1);

代码正常工作,但我不知道禁用帖子的价值 > 如果我禁用帖子,计数将重置为 1,并且该帖子不算数。

我想要2个解决方案:

  1. 如何计算所有状态?

  2. 我怎样才能得到最后的价值并加+1?

谢谢

wordpress count
1个回答
-1
投票
add_action('save_post', 'save_post_callback');
function save_post_callback($post_id) {
    $post_type = get_post_type($post_id);

    if ($post_type === 'estate_property') {
        $no_of_posts = wp_count_posts('estate_property');
        if (get_post_meta($post_id, 'prop', true) === '' && !wp_is_post_revision($post_id)) {
            $_POST['prop'] = $no_of_posts->publish + $no_of_posts->draft + 1;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.