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:
-
How can I count all statuses?
-
How can I get last value and add +1?
Thanks
3
Answers
The solution atm is
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)
You could just count all statuses for that post type using a SQL query like so: