I found a little trick that I’ve overlooked so many times when writing plugins that use custom post types. Did you know that you can do this?
1 2 3 4 5 6 7 8 9 10 | $args = [ 'post_type' => 'books' , 'post_title' => 'An Exciting Look at Mud' , 'post_content' => 'Things and stuff and content. Probably involving that book.' , 'meta_input' => [ 'book_author' => 'Mr. Bookwriter Dude' , 'authors_fav_quote' => "I have a belly button and HERE IT IS!" ] ]; $post_id = wp_insert_post( $args ); |
How cool is that?! Now don’t laugh at me, but I looked for my postmeta in my DB because I’m always skeptical and look at this beauty:
1 2 3 4 5 6 7 8 | mysql> select * from wp_postmeta where post_id=742444; +---------+---------+-------------------+---------------------------------------+ | meta_id | post_id | meta_key | meta_value | +---------+---------+-------------------+---------------------------------------+ | 2779343 | 742444 | book_author | Mr. Bookwriter Dude | | 2779344 | 742444 | authors_fav_quote | I have a belly button and HERE IT IS! | +---------+---------+-------------------+---------------------------------------+ 2 rows in set (0.00 sec) |
Nice
Cheers!
Ryan