skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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. 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;
            }
        }
    }
    
    Login or Signup to reply.
  3. 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'"
        )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search