* 4 * If the $postarr parameter has 'ID' set to a value, then post will be updated. 5 * 6 * You can set the post date manually, by setting the values for 'post_date' 7 * and 'post_date_gmt' keys. You can close the comments or open the comments by 8 * setting the value for 'comment_status' key. 9 * 10 * @since 1.0.0 11 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. 12 * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.t 13 * 14 * @see sanitize_post() 15 * @global wpdb $wpdb WordPress database abstraction object. 16 * 17 * @param array $postarr { 18 * An array of elements that make up a post to update or insert. 19 * 20 * @type int $ID The post ID. If equal to something other than 0, 21 * the post with that ID will be updated. Default 0. 22 * @type int $post_author The ID of the user who added the post. Default is 23 * the current user ID. 24 * @type string $post_date The date of the post. Default is the current time. 25 * @type string $post_date_gmt The date of the post in the GMT timezone. Default is 26 * the value of `$post_date`. 27 * @type mixed $post_content The post content. Default empty. 28 * @type string $post_content_filtered The filtered post content. Default empty. 29 * @type string $post_title The post title. Default empty. 30 * @type string $post_excerpt The post excerpt. Default empty. 31 * @type string $post_status The post status. Default 'draft'. 32 * @type string $post_type The post type. Default 'post'. 33 * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. 34 * Default is the value of 'default_comment_status' option. 35 * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. 36 * Default is the value of 'default_ping_status' option. 37 * @type string $post_password The password to access the post. Default empty. 38 * @type string $post_name The post name. Default is the sanitized post title 39 * when creating a new post. 40 * @type string $to_ping Space or carriage return-separated list of URLs to ping. 41 * Default empty.
* Fires once a post has been saved. 602 * 603 * @since 2.0.0 604 * 605 * @param int $post_ID Post ID. 606 * @param WP_Post $post Post object. 607 * @param bool $update Whether this is an 608 */ 609 do_action( 'wp_insert_post', $post_ID, $post, 610 611 return $post_ID; 612 }
* The date does not have to be set for drafts. You can set the date and it will * not be overridden. * * @since 1.0.0 * * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, * objects are not. Default array. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. */ function wp_update_post( $postarr = array(), $wp_error = false ) { // Logic Checks. // Smart Defaults return wp_insert_post( $postarr, $wp_error ); }
* The date does not have to be set for drafts. You can set the date and it will * not be overridden. * * @since 1.0.0 * * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, * objects are not. Default array. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. */ function wp_update_post( $postarr = array(), $wp_error = false ) { // Logic Checks. // Smart Defaults return wp_insert_post( $postarr, $wp_error ); } // First, get all of the original fields. $post = get_post( $postarr['ID'], ARRAY_A ); if ( is_null( $post ) ) { /* ... */ return 0; }
* The date does not have to be set for drafts. You can set the date and it will * not be overridden. * * @since 1.0.0 * * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, * objects are not. Default array. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. */ function wp_update_post( $postarr = array(), $wp_error = false ) { // Logic Checks. // Smart Defaults return wp_insert_post( $postarr, $wp_error ); } // First, get all of the original fields. $post = get_post( $postarr['ID'], ARRAY_A ); if ( is_null( $post ) ) { /* ... */ return 0; }
inserted into * the database. * * @since 2.7.0 * * @param array $data An array of slashed post data. * @param array $postarr An array of sanitized, but * otherwise unmodified post data. */ $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
inserted into * the database. * * @since 2.7.0 * * @param array $data An array of slashed post data. * @param array $postarr An array of sanitized, but * otherwise unmodified post data. */ $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
inserted into * the database. * * @since 2.7.0 * * @param array $data An array of slashed post data. * @param array $postarr An array of sanitized, but * otherwise unmodified post data. */ $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
inserted into * the database. * * @since 2.7.0 * * @param array $data An array of slashed post data. * @param array $postarr An array of sanitized, but * otherwise unmodified post data. */ $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
an existing post is updated in the database. * * @since 2.5.0 * * @param int $post_ID Post ID. * @param array $data Array of unslashed post data. */ do_action( 'pre_post_update', $post_ID, $data ); if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { if ( $wp_error ) { return new WP_Error( 'db_update_error', __( 'Could not update post in the } else { return 0; } } } else { // If there is a suggested ID, use it if not already present. if ( ! empty( $import_id ) ) { $import_id = (int) $import_id; if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID
$data Array of unslashed post data. */ do_action( 'pre_post_update', $post_ID, $data ); if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { if ( $wp_error ) { return new WP_Error( 'db_update_error', __( 'Could not update post in the } else { return 0; } } } else { // If there is a suggested ID, use it if not already present. if ( ! empty( $import_id ) ) { $import_id = (int) $import_id; if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID $data['ID'] = $import_id; } } if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { if ( $wp_error ) { return new WP_Error( 'db_insert_error', __( 'Could not insert post into th } else {
inserted into * the database. * * @since 2.7.0 * * @param array $data An array of slashed post data. * @param array $postarr An array of sanitized, but * otherwise unmodified post data. */ $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
@since 2.8.0 * * @see wp_set_object_terms() * * @param int $post_id Optional. The Post ID. * Does not default to the ID of the global $post. * @param string|array $tags Optional. An array of terms to set for the post, or a * string of terms separated by commas. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. * @param bool $append Optional. If true, don't delete existing terms, just add on. * If false, replace the terms with the new terms. Default false. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. * WP_Error or false on failure. */ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { }
an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @since 2.3.0 * * @global wpdb $wpdb The WordPress database abstraction object. * * @param int $object_id The object to relate to. * @param string|int|array $terms A single term slug, single term id, or array of * either term slugs or ids. Will replace all existing * related terms in this taxonomy. Passing an empty * value will remove all related terms. * @param string $taxonomy The context in which to relate the term to * the object. * @param bool $append Optional. If false will delete difference of terms. * Default false. * @return array|WP_Error Term taxonomy IDs of the affected terms. */ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { }
@since 2.8.0 * * @see wp_set_object_terms() * * @param int $post_id Optional. The Post ID. * Does not default to the ID of the global $post. * @param string|array $tags Optional. An array of terms to set for the post, or a * string of terms separated by commas. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. * @param bool $append Optional. If true, don't delete existing terms, just add on. * If false, replace the terms with the new terms. Default false. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. * WP_Error or false on failure. */ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { }
@since 2.8.0 * * @see wp_set_object_terms() * * @param int $post_id Optional. The Post ID. * Does not default to the ID of the global $post. * @param string|array $tags Optional. An array of terms to set for the post, or a * string of terms separated by commas. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. * @param bool $append Optional. If true, don't delete existing terms, just add on. * If false, replace the terms with the new terms. Default false. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. * WP_Error or false on failure. */ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { }
than names so that children with the same names but * different parents aren't confused. */ if ( is_taxonomy_hierarchical( $taxonomy ) ) { $tags = array_unique( array_map( 'intval', $tags ) ); }
an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @since 2.3.0 * * @global wpdb $wpdb The WordPress database abstraction object. * * @param int $object_id The object to relate to. * @param string|int|array $terms A single term slug, single term id, or array of * either term slugs or ids. Will replace all existing * related terms in this taxonomy. Passing an empty * value will remove all related terms. * @param string $taxonomy The context in which to relate the term to * the object. * @param bool $append Optional. If false will delete difference of terms. * Default false. * @return array|WP_Error Term taxonomy IDs of the affected terms. */ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { }
an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @since 2.3.0 * * @global wpdb $wpdb The WordPress database abstraction object. * * @param int $object_id The object to relate to. * @param string|int|array $terms A single term slug, single term id, or array of * either term slugs or ids. Will replace all existing * related terms in this taxonomy. Passing an empty * value will remove all related terms. * @param string $taxonomy The context in which to relate the term to * the object. * @param bool $append Optional. If false will delete difference of terms. * Default false. * @return array|WP_Error Term taxonomy IDs of the affected terms. */ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { } Fascinating but way, way, way beyond the scope f this talk
value already exists for the specified object * ID and metadata key, the metadata will be added. * * @since 2.9.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) * @param int $object_id ID of the object metadata is for * @param string $meta_key Metadata key * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. If specified, only update existing metadata * entries with the specified value. Otherwise, update all entries. * @return int|bool Meta ID if the key didn't exist, true on successful update, *. false on failure. */ function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). Returning a non-null value * will effectively short-circuit the function. * * @since 3.1.0 * * @param null|bool $check Whether to allow updating metadata for the * given type. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. If specified, only update existing * metadata entries with the specified value. * Otherwise, update all entries. */ $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); if ( null !== $check ) { return (bool) $check; }
type. * * The dynamic portion of the hook, `$meta_type`, refers to the meta * object type (comment, post, or user). Returning a non-null value * will effectively short-circuit the function. * * @since 3.1.0 * * @param null|bool $check Whether to allow updating metadata for the * given type. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. Must be serializable if non-scalar. * @param mixed $prev_value Optional. If specified, only update existing * metadata entries with the specified value. * Otherwise, update all entries. */ $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); if ( null !== $check ) { return (bool) $check; } "update_post_metadata",
( rand( 0, 1 ) ) { // Allow function to run. return null; } // Do not allow function to run. return false; } add_filter( 'update_post_metadata', 'pw_update_meta_filter' );
( rand( 0, 1 ) ) { // Allow function to run. return null; } // Do not allow function to run. return false; } add_filter( 'update_post_metadata', 'pw_update_meta_filter' );
( rand( 0, 1 ) ) { // Allow function to run. return null; } // Do not allow function to run. return false; } add_filter( 'update_post_metadata', 'pw_update_meta_filter' );
( rand( 0, 1 ) ) { // Allow function to run. return null; } // Do not allow function to run. return false; } add_filter( 'update_post_metadata', 'pw_update_meta_filter' );