Slide 1

Slide 1 text

wp_insert_post(): How, what and why? Peter Wilson • @pwcc • peterwilson.cc

Slide 2

Slide 2 text

1 /** 2 * Insert or update a post. 3 * 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.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

598 do_action( 'save_post', $post_ID, $post, $upda 599 600 /** 601 * 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 }

Slide 5

Slide 5 text

php > var_dump( PHP_INT_MAX );

Slide 6

Slide 6 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)


Slide 7

Slide 7 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 );

Slide 8

Slide 8 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 ); float(1.844674407371E+19)


Slide 9

Slide 9 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 ); float(1.844674407371E+19)
 php > var_dump( 18446744073709551615 > PHP_INT_MAX );

Slide 10

Slide 10 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 ); float(1.844674407371E+19)
 php > var_dump( 18446744073709551615 > PHP_INT_MAX ); bool(true)


Slide 11

Slide 11 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 ); float(1.844674407371E+19)
 php > var_dump( 18446744073709551615 > PHP_INT_MAX ); bool(true)
 php > var_dump( (int) 18446744073709551615 );

Slide 12

Slide 12 text

php > var_dump( PHP_INT_MAX ); int(9223372036854775807)
 php > var_dump( 18446744073709551615 ); float(1.844674407371E+19)
 php > var_dump( 18446744073709551615 > PHP_INT_MAX ); bool(true)
 php > var_dump( (int) 18446744073709551615 ); int(0)

Slide 13

Slide 13 text

/** * Update a post with new post data. * * 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 ); }

Slide 14

Slide 14 text

/** * Update a post with new post data. * * 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; }

Slide 15

Slide 15 text

/** * Update a post with new post data. * * 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; }

Slide 16

Slide 16 text

Permission checks wp_insert_post() skips them.

Slide 17

Slide 17 text

if ( current_user_can( 'edit_posts' ) ) { wp_insert_post( [] ); }

Slide 18

Slide 18 text

/* if ( current_user_can( 'edit_posts' ) ) { */ wp_insert_post( [] ); }

Slide 19

Slide 19 text

$post_type_object = get_post_type_object( 'post' ); if ( current_user_can( $post_type_object->cap->create_posts ) ) { /* if ( current_user_can( 'edit_posts' ) ) { */ wp_insert_post( [] ); }

Slide 20

Slide 20 text

$post_type_object = get_post_type_object( 'post' ); if ( current_user_can( $post_type_object->cap->create_posts ) ) { /* if ( current_user_can( 'edit_posts' ) ) { */ wp_insert_post( [] ); }

Slide 21

Slide 21 text

$post_type_object = get_post_type_object( 'post' ); if ( current_user_can( $post_type_object->cap->create_posts ) ) { /* if ( current_user_can( 'edit_posts' ) ) { */ wp_insert_post( [] ); }

Slide 22

Slide 22 text

$post_type_object = get_post_type_object( 'slide_deck' ); if ( current_user_can( $post_type_object->cap->create_posts ) ) { /* if ( current_user_can( 'edit_slide_decks' ) ) { */ wp_insert_post( [] ); }

Slide 23

Slide 23 text

$post_type_object = get_post_type_object( 'slide_deck' ); if ( current_user_can( $post_type_object->cap->create_posts ) ) { /* if ( current_user_can( 'create_slide_decks' ) ) { */ wp_insert_post( [] ); }

Slide 24

Slide 24 text

if ( current_user_can( 'edit_post', $post_id ) ) { wp_insert_post( [] ); }

Slide 25

Slide 25 text

$post_type_object = get_post_type_object( 'post' ); if ( current_user_can( $post_type_object->cap->edit_post, $post_id ) ) { /* if ( current_user_can( 'edit_post', $post_id ) ) { */ wp_insert_post( [] ); }

Slide 26

Slide 26 text

// Don't allow contributors to set the // post slug for pending review posts. if ( 'pending' == $post_status && ! current_user_can( 'publish_posts' ) ) { $post_name = ''; }

Slide 27

Slide 27 text

In the beginning

Slide 28

Slide 28 text

function wp_insert_post( $postarr, $wp_error = false ) { global $wpdb; $user_id = get_current_user_id(); $defaults = array( 'post_author' => $user_id, 'post_content' => '', 'post_content_filtered' => '', 'post_title' => '', 'post_excerpt' => '', 'post_status' => 'draft', 'post_type' => 'post', 'comment_status' => '', 'ping_status' => '', 'post_password' => '', 'to_ping' => '', 'pinged' => '', 'post_parent' => 0, 'menu_order' => 0, 'guid' => '', 'import_id' => 0, 'context' => '', ); $postarr = wp_parse_args( $postarr, $defaults ); unset( $postarr['filter'] );

Slide 29

Slide 29 text

$postarr = wp_parse_args( $postarr, $defaults ); unset( $postarr['filter'] ); $postarr = sanitize_post( $postarr, 'db' ); // Are we updating or creating? $post_ID = 0; $update = false; $guid = $postarr['guid']; if ( ! empty( $postarr['ID'] ) ) { $update = true; // Get the post ID and GUID. $post_ID = $postarr['ID']; $post_before = get_post( $post_ID ); if ( is_null( $post_before ) ) { if ( $wp_error ) { return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); } return 0; } $guid = get_post_field( 'guid', $post_ID ); $previous_status = get_post_field( 'post_status', $post_ID ); } else { $previous_status = 'new'; }

Slide 30

Slide 30 text

$postarr = wp_parse_args( $postarr, $defaults ); unset( $postarr['filter'] ); $postarr = sanitize_post( $postarr, 'db' ); // Are we updating or creating? $post_ID = 0; $update = false; $guid = $postarr['guid']; if ( ! empty( $postarr['ID'] ) ) { $update = true; // Get the post ID and GUID. $post_ID = $postarr['ID']; $post_before = get_post( $post_ID ); if ( is_null( $post_before ) ) { if ( $wp_error ) { return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); } return 0; } $guid = get_post_field( 'guid', $post_ID ); $previous_status = get_post_field( 'post_status', $post_ID ); } else { $previous_status = 'new'; } oring

Slide 31

Slide 31 text

/** * Filters slashed post data just before it is 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 );

Slide 32

Slide 32 text

/** * Filters slashed post data just before it is 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 );

Slide 33

Slide 33 text

/** * Filters slashed post data just before it is 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 );

Slide 34

Slide 34 text

/** * Add "sincerely, -me" to the end of each post before saving. * * @param array $data An array of slashed post data. * @return array A modified array of slashed post data. */ function add_evan_hansen_reference( $data ) { $data = wp_unslash( $data ); $data['post_content'] .= "\n\nsincerely,\n-me"; return wp_slash( $data ); } add_filter( 'wp_insert_post_data', 'add_evan_hansen_reference' );

Slide 35

Slide 35 text

/** * Add "sincerely, -me" to the end of each post before saving. * * @param array $data An array of slashed post data. * @return array A modified array of slashed post data. */ function add_evan_hansen_reference( $data ) { $data = wp_unslash( $data ); $data['post_content'] .= "\n\nsincerely,\n-me"; return wp_slash( $data ); } add_filter( 'wp_insert_post_data', 'add_evan_hansen_reference' );

Slide 36

Slide 36 text

/** * Add "sincerely, -me" to the end of each post before saving. * * @param array $data An array of slashed post data. * @return array A modified array of slashed post data. */ function add_evan_hansen_reference( $data ) { $data = wp_unslash( $data ); $data['post_content'] .= "\n\nsincerely,\n-me"; return wp_slash( $data ); } add_filter( 'wp_insert_post_data', 'add_evan_hansen_reference' );

Slide 37

Slide 37 text

/** * Add "sincerely, -me" to the end of each post before saving. * * @param array $data An array of slashed post data. * @return array A modified array of slashed post data. */ function add_evan_hansen_reference( $data ) { $data = wp_unslash( $data ); $data['post_content'] .= "\n\nsincerely,\n-me"; return wp_slash( $data ); } add_filter( 'wp_insert_post_data', 'add_evan_hansen_reference' );

Slide 38

Slide 38 text

return wp_slash( $data );

Slide 39

Slide 39 text

$data = wp_slash( $data ); return $data;

Slide 40

Slide 40 text

$data = wp_slash( $data ); $data['post_title'] .= " ¯\_(ϑ)_/¯"; return $data;

Slide 41

Slide 41 text

$data = wp_slash( $data ); $data['post_title'] .= " ¯\_(ϑ)_/¯"; return $data;

Slide 42

Slide 42 text

$data = wp_slash( $data ); return $data;

Slide 43

Slide 43 text

return wp_slash( $data );

Slide 44

Slide 44 text

$data['post_title'] .= " ¯\_(ϑ)_/¯”; return wp_slash( $data );

Slide 45

Slide 45 text

echo esc_url( $data );

Slide 46

Slide 46 text

/** * Filters slashed post data just before it is 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 );

Slide 47

Slide 47 text

if ( $update ) { /** * Fires immediately before 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

Slide 48

Slide 48 text

* * @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['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 {

Slide 49

Slide 49 text

/** * Filters slashed post data just before it is 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 );

Slide 50

Slide 50 text

if ( is_object_in_taxonomy( $post_type, 'category' ) ) { wp_set_post_categories( $post_ID, $post_category ); } if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { wp_set_post_tags( $post_ID, $postarr['tags_input'] ); }

Slide 51

Slide 51 text

wp_set_post_categories( $post_ID, $post_category ); wp_set_post_tags( $post_ID, $postarr['tags_input'] );

Slide 52

Slide 52 text

/** * Set the terms for a post. * * @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 ) { }

Slide 53

Slide 53 text

/** * Create Term and Taxonomy Relationships. * * Relates 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 ) { }

Slide 54

Slide 54 text

wp_set_post_categories( $post_ID, $post_category )

Slide 55

Slide 55 text

if ( 'post' == $post_type && 'auto-draft' != $post_status ) { $post_categories = array( get_option('default_category') ); $append = false; } else { $post_categories = array(); }

Slide 56

Slide 56 text

function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { return wp_set_post_terms( $post_id, $tags, 'post_tag', $append); }

Slide 57

Slide 57 text

/** * Set the terms for a post. * * @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 ) { }

Slide 58

Slide 58 text

Non-hierarchical taxonomy (eg tags) wp_set_post_terms( 12, // Post ID. [ 'apple', 'microsoft' ],// Terms (slugs). 'post_tag' // Taxonomy. );

Slide 59

Slide 59 text

Non-hierarchical taxonomy (eg tags) wp_set_post_terms( 12, // Post ID. [ 12, 120 ], // Terms (IDs). 'post_tag' // Taxonomy. );

Slide 60

Slide 60 text

Non-hierarchical taxonomy (eg tags) wp_set_post_terms( 12, // Post ID. [ 'apple', 'microsoft' ],// Terms (slugs). 'post_tag' // Taxonomy. );

Slide 61

Slide 61 text

Hierarchical taxonomy (eg categories)

Slide 62

Slide 62 text

Categories • Phones • Android • Apple • Laptops • Apple • Microsoft

Slide 63

Slide 63 text

Categories • Phones (phones) • Android (android) • Apple (apple) • Laptops (laptop) • Apple (apple) • Microsoft (microsoft)

Slide 64

Slide 64 text

Hierarchical taxonomy (eg categories) wp_set_post_terms( 12, // Post ID. [ 'apple', 'microsoft' ],// Terms (slugs). 'category' // Taxonomy. );

Slide 65

Slide 65 text

Hierarchical taxonomy (eg categories) wp_set_post_terms( 12, // Post ID. [ 12, 120 ], // Terms (IDs). 'category' // Taxonomy. );

Slide 66

Slide 66 text

/** * Set the terms for a post. * * @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 ) { }

Slide 67

Slide 67 text

/* * Hierarchical taxonomies must always pass IDs rather * 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 ) ); }

Slide 68

Slide 68 text

/** * Create Term and Taxonomy Relationships. * * Relates 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 ) { }

Slide 69

Slide 69 text

/** * Create Term and Taxonomy Relationships. * * Relates 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

Slide 70

Slide 70 text

// New-style support for all custom taxonomies. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { $taxonomy_obj = get_taxonomy( $taxonomy ); if ( ! $taxonomy_obj ) { _doing_it_wrong( /* snip */ ); continue; } // array = hierarchical, string = non-hierarchical. if ( is_array( $tags ) ) { $tags = array_filter( $tags ); } if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { wp_set_post_terms( $post_ID, $tags, $taxonomy ); } }

Slide 71

Slide 71 text

// New-style support for all custom taxonomies. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { $taxonomy_obj = get_taxonomy( $taxonomy ); if ( ! $taxonomy_obj ) { _doing_it_wrong( /* snip */ ); continue; } // array = hierarchical, string = non-hierarchical. if ( is_array( $tags ) ) { $tags = array_filter( $tags ); } if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { wp_set_post_terms( $post_ID, $tags, $taxonomy ); } }

Slide 72

Slide 72 text

// New-style support for all custom taxonomies. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { $taxonomy_obj = get_taxonomy( $taxonomy ); if ( ! $taxonomy_obj ) { _doing_it_wrong( /* snip */ ); continue; } // array = hierarchical, string = non-hierarchical. if ( is_array( $tags ) ) { $tags = array_filter( $tags ); } if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { wp_set_post_terms( $post_ID, $tags, $taxonomy ); } }

Slide 73

Slide 73 text

Post meta

Slide 74

Slide 74 text

wp_insert_post( [ 'post_type' => 'musical', 'post_title' => 'Dear Evan Hansen', 'meta_input' => [ 'Male lead' => 'Ben Platt', 'Female lead' => 'Laura Dreyfuss', ], ] );

Slide 75

Slide 75 text

if ( ! empty( $postarr['meta_input'] ) ) { foreach ( $postarr['meta_input'] as $field => $value ) { update_post_meta( $post_ID, $field, $value ); } }

Slide 76

Slide 76 text

update_post_meta( $post_ID, 'Male Lead', 'Ben Platt', ‘Ben Platt' ); update_post_meta( $post_ID, 'Male Lead', 'Noah Galvin', 'Noah Galvin' ); update_post_meta( $post_ID, 'Male Lead', 'Taylor Trensch', 'Taylor Trensch' );

Slide 77

Slide 77 text

/** * Update metadata for the specified object. If no 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 = '') {

Slide 78

Slide 78 text

/** * Filters whether to update metadata of a specific 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; }

Slide 79

Slide 79 text

/** * Filters whether to update metadata of a specific 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",

Slide 80

Slide 80 text

Slide 81

Slide 81 text

Slide 82

Slide 82 text

Slide 83

Slide 83 text

Slide 84

Slide 84 text

Slide 85

Slide 85 text

Slide 86

Slide 86 text

Slide 87

Slide 87 text

Slide 88

Slide 88 text

Wrapping up Actions and filters

Slide 89

Slide 89 text

if ( $update ) { do_action( 'edit_post', $post_ID, $post ); $post_after = get_post( $post_ID ); do_action( 'post_updated', $post_ID, $post_after, $post_before ); } Post revision saved.

Slide 90

Slide 90 text

do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); do_action( 'save_post', $post_ID, $post, $update ); do_action( 'wp_insert_post', $post_ID, $post, $update );

Slide 91

Slide 91 text

return $postID; }

Slide 92

Slide 92 text

return $postID; } // Any Questions?