{"action":"created","issue":{"url":"https://api.github.com/repos/wp-cli/wp-cli/issues/1960","labels_url":"https://api.github.com/repos/wp-cli/wp-cli/issues/1960/labels{/name}","comments_url":"https://api.github.com/repos/wp-cli/wp-cli/issues/1960/comments","events_url":"https://api.github.com/repos/wp-cli/wp-cli/issues/1960/events","html_url":"https://github.com/wp-cli/wp-cli/issues/1960","id":98799224,"number":1960,"title":"Implement wp_defer_term_counting()","user":{"login":"dougaxe1","id":10548206,"avatar_url":"https://avatars.githubusercontent.com/u/10548206?v=3","gravatar_id":"","url":"https://api.github.com/users/dougaxe1","html_url":"https://github.com/dougaxe1","followers_url":"https://api.github.com/users/dougaxe1/followers","following_url":"https://api.github.com/users/dougaxe1/following{/other_user}","gists_url":"https://api.github.com/users/dougaxe1/gists{/gist_id}","starred_url":"https://api.github.com/users/dougaxe1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougaxe1/subscriptions","organizations_url":"https://api.github.com/users/dougaxe1/orgs","repos_url":"https://api.github.com/users/dougaxe1/repos","events_url":"https://api.github.com/users/dougaxe1/events{/privacy}","received_events_url":"https://api.github.com/users/dougaxe1/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-08-03T17:32:35Z","updated_at":"2015-08-10T13:06:48Z","closed_at":null,"body":"Batch post and term operations could be sped up significantly by enabling [wp_defer_term_counting()](http://codex.wordpress.org/Function_Reference/wp_defer_term_counting). A new `--defer_term_counting` flag could enable this performance increase.\r\n\r\n````\r\nwp post delete $(wp post list --post_type='page' --format=ids) --force --defer_term_counting\r\n````\r\n\r\n````php\r\n\t/**\r\n\t * Delete a post by ID.\r\n\t *\r\n\t * ## OPTIONS\r\n\t *\r\n\t * <id>...\r\n\t * : One or more IDs of posts to delete.\r\n\t *\r\n\t * [--force]\r\n\t * : Skip the trash bin.\r\n\t *\r\n\t * [--defer-term-counting]\r\n\t * : Defers term counting until all posts are deleted\r\n\t *\r\n\t * ## EXAMPLES\r\n\t *\r\n\t *     wp post delete 123 --force\r\n\t *\r\n\t *     wp post delete $(wp post list --post_type='page' --format=ids)\r\n\t *\r\n\t *     # delete all posts in the trash\r\n\t *     wp post delete $(wp post list --post_status=trash --format=ids)\r\n\t *\r\n\t *     wp post delete $(wp post list --post_type='page' --format=ids) --force --defer_term_counting\r\n\t */\r\n\tpublic function delete( $args, $assoc_args ) {\r\n\t\t$defaults = array(\r\n\t\t\t'force' => false,\r\n\t\t\t'defer_term_counting' => false\r\n\t\t);\r\n\t\t$assoc_args = array_merge( $defaults, $assoc_args );\r\n\r\n\t\tif ( $assoc_args['defer_term_counting'] ) { wp_defer_term_counting(true); }\r\n\t\t\r\n\t\tparent::_delete( $args, $assoc_args, function ( $post_id, $assoc_args ) {\r\n\t\t\t$status = get_post_status( $post_id );\r\n\t\t\t$r = wp_delete_post( $post_id, $assoc_args['force'] );\r\n\r\n\t\t\tif ( $r ) {\r\n\t\t\t\t$action = $assoc_args['force'] || 'trash' === $status ? 'Deleted' : 'Trashed';\r\n\r\n\t\t\t\treturn array( 'success', \"$action post $post_id.\" );\r\n\t\t\t} else {\r\n\t\t\t\treturn array( 'error', \"Failed deleting post $post_id.\" );\r\n\t\t\t}\r\n\t\t} );\r\n\r\n\t\tif ( $assoc_args['defer_term_counting'] ) { wp_defer_term_counting(false); }\r\n\t}\r\n````\r\n\r\nThanks to @davideve for pointing out this performance bottleneck.\r\n\r\nI can create a pull request for this at some point in the future, but I just wanted to put this out there to see if someone else was interested in implementing. Or in case this is a bad idea."},"comment":{"url":"https://api.github.com/repos/wp-cli/wp-cli/issues/comments/129435479","html_url":"https://github.com/wp-cli/wp-cli/issues/1960#issuecomment-129435479","issue_url":"https://api.github.com/repos/wp-cli/wp-cli/issues/1960","id":129435479,"user":{"login":"danielbachhuber","id":36432,"avatar_url":"https://avatars.githubusercontent.com/u/36432?v=3","gravatar_id":"","url":"https://api.github.com/users/danielbachhuber","html_url":"https://github.com/danielbachhuber","followers_url":"https://api.github.com/users/danielbachhuber/followers","following_url":"https://api.github.com/users/danielbachhuber/following{/other_user}","gists_url":"https://api.github.com/users/danielbachhuber/gists{/gist_id}","starred_url":"https://api.github.com/users/danielbachhuber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielbachhuber/subscriptions","organizations_url":"https://api.github.com/users/danielbachhuber/orgs","repos_url":"https://api.github.com/users/danielbachhuber/repos","events_url":"https://api.github.com/users/danielbachhuber/events{/privacy}","received_events_url":"https://api.github.com/users/danielbachhuber/received_events","type":"User","site_admin":false},"created_at":"2015-08-10T13:06:48Z","updated_at":"2015-08-10T13:06:48Z","body":"> `wp_defer_term_counting(false)` will trigger the recount, but in batch (deduped) as opposed to each item.\r\n\r\nGreat.\r\n\r\n> I don't know if I would get to it this soon, but working on a PR I could run some benchmarks.\r\n\r\n:+1: "}}