Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Created September 2, 2013 17:49
Show Gist options
  • Save DrewAPicture/6415461 to your computer and use it in GitHub Desktop.
Save DrewAPicture/6415461 to your computer and use it in GitHub Desktop.
Update post fields (counter part to get_post_field()
<?php
/**
* Update one or more post fields
*
* @global wpdb $wpdb
*
* @param array $field Array of field/value pairs to update.
* @param WP_Post|int $post Post object or id.
*
* @return int|bool Number of updated rows, false on failure.
*/
function update_post_fields( $fields, $post ) {
global $wpdb;
if ( ! is_array( $fields ) || empty( $fields ) )
return;
if ( ! is_a( $post, 'WP_Post' ) )
$post = get_post( $post );
$rows = $wpdb->update( $wpdb->posts, $fields, array( 'ID' => $post->ID ) );
return $rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment