如何计算自自定义文章类型(CPT)的所有帖子?

huangapple go评论61阅读模式
英文:

How to count all posts from a CPT?

问题

你的WordPress房地产主题有一个用于属性的自定义文章类型。

后端的状态有:全部,我的,已发布,草稿,用户禁用。

我有一个自定义字段(prop),我想在发布新文章时自动完成它。
WordPress的ID计数不是按1递增的。所以我想要使用该字段来自动完成它(文章数量+1)。

我有这个函数。

/**  当发布新的房地产属性时生成自定义ID。*/
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?

谢谢。

英文:

My wordpress real estate theme have a custom post type for properties.

Statuses in backend: All, Mine, Published, Draft, Disabled by user

I have a custom field (prop) and I am trying to autocomplete it when a new post is published.
Wordpress id counting is not ascending by 1. So I want to use that field to autocomplete it with (no of posts + 1)

I have this function.

/**   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);
}

I tried

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

and the code is working, but I don't know the value for disabled posts > if I disable a post, the count reset by 1 and that post doesn't count to it.

I was thinking on 2 solutions:

  1. How can I count all statuses?

  2. How can I get last value and add +1?

Thanks

答案1

得分: 1

解决方案目前是

$_POST["mls"] = ($no_of_posts->publish + $no_of_posts->draft + $no_of_posts->disabled);

我之前在测试时使用的是 'disable'(没有 d)。

但仍在寻找一种方法,使该值等于上一篇帖子的值 +1(如果用户删除帖子,则计数会更改)。

英文:

The solution atm is

$_POST["mls"] = ($no_of_posts->publish + $no_of_posts->draft + $no_of_posts->disabled);

I was testing with 'disable' before (without d).

But still looking for a way to have the value from last post +1 (if user delete posts the count changes)

答案2

得分: 0

你可以使用以下的SQL查询来统计该帖子类型的所有状态:

global $wpdb;

$posts_count = $wpdb->prepare(
    $wpdb->get_var(
        "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'estate_property'"
    )
);
英文:

You could just count all statuses for that post type using a SQL query like so:

global $wpdb;


$posts_count = $wpdb->prepare(
    $wpdb->get_var(
        "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'estate_property'"
    )
);

答案3

得分: -1

Here is the translated code:

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;
        }
    }
}

Please note that code translations may sometimes require context, so ensure that it functions as expected in your specific code environment.

英文:
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;
        }
    }
}

huangapple
  • 本文由 发表于 2023年5月17日 17:19:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定