Delete all posts of a custom post type—efficiently

You can delete all post via $wpdb DELETE FROM wp_posts WHERE post_type=”post_type”; DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts); DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts) or use this query replace it with {{your CPT}} with your Custom Post Type DELETE a,b,c FROM wp_posts a LEFT JOIN … Read more

How do I set the default admin sort order for a custom post type to a custom column?

Solution from a cross post over at StackExchange from @birgire: The problem is that you run the clientarea_default_order callback too late. To fix that you only have to change the priority from the default one that’s 10: add_action( ‘pre_get_posts’,’clientarea_default_order’); to the priority of 9: add_action( ‘pre_get_posts’,’clientarea_default_order’, 9 ); But you don’t actually need two pre_get_posts … Read more

How do I save metadata for a specific custom post type only?

function save_my_metadata($ID = false, $post = false) { if($post->post_type != ‘your_post_type’) return; update_post_meta($ID, ‘my_metadata’, $_POST[‘my_metadata’]); } That should work. Just replace ‘your_post_type’ with the name of the post type. Also, little known fact: the ‘save_post’ hook passes the post’s ID as an argument. EDIT I updated the function to reflect Jan’s comment. Thanks Jan!