Monday, July 9, 2012

Perfect realization of the wordpress post ID consecutive


Although WordPress 3.1.3 has been to "revision" function-per-click "add new post" or "Add a new page" will generate an "auto  draft", occupies a single ID.


If you do not enter something, then this draft in WP background is invisible.
In order to use this ID, it is necessary to operate the database, which is too much trouble, and error-prone.


Is there a simple way?
Haha, Yes,of course.

WordPress 3.1.3 has removed the "revision" feature, so now want to implement Article ID consecutive easy.
Specific methods are as follows:
In wp-admin/includes/post.php, find "create_in_db", find the following code:



 if ( $create_in_db ) {
  $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
  $post = get_post( $post_id );
  if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
   set_post_format( $post, get_option( 'default_post_format' ) );
 } else {


Revised as


if ( $create_in_db ) {
        global $current_user;
        $post_auto_draft = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = 'auto-draft' AND post_author = $current_user->ID ORDER BY ID ASC LIMIT 1" );
        if ($post_auto_draft){
            $post = $post_auto_draft;
        } else {
            $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
            $post = get_post( $post_id );
        }
} else {



Modified when one or more post status is "auto-draft" in database .click on "Add a new post" will automatically call the smallest ID. Only when the database have not data of this type (auto-draft) will be to insert a new data.


It can avoid the generation of "auto draft" occupier ID undermine the continuity of the ID, and also reduce unnecessary trouble to directly modify the database.



Note:


* Directly from the background to upload media files (pictures and various attachment) will occupy one ID. I recommended to use FTP to upload or use outside the chain.



Using the theme  "custom menu", each add a menu to the navigation will occupy an ID, not recommended.




1 comment:

  1. wordpress services I do agree with all the points you have presented in your post. You’ll be able to definitely see your excitement inside the pieces you write

    ReplyDelete