How to set post slug when using wp_insert_post();?
The parameter to insert a custom slug is: ‘post_name’ => ‘my-custom-slug’ Not post_slug as one would think! 🙂
The parameter to insert a custom slug is: ‘post_name’ => ‘my-custom-slug’ Not post_slug as one would think! 🙂
If you don’t add a post_date then WordPress fills it automatically with the current date and time. To set another date and time [ Y-m-d H:i:s ] is the right structure. An example below with your code. $postdate=”2010-02-23 18:57:33″; $new_post = array( ‘post_title’ => $title, ‘post_content’ => $description, ‘post_date’ => $postdate, ‘post_status’ => ‘publish’, ‘post_parent’ … Read more
Here’s a way to do it with PHP’s array map function: // Good idea to make sure things are set before using them $tags = isset( $_POST[‘tags’] ) ? (array) $_POST[‘tags’] : array(); // Any of the WordPress data sanitization functions can be used here $tags = array_map( ‘esc_attr’, $tags );
Can’t this simply be done with media_sideload_image() ? Seems pretty simple. Only catch is if you aren’t on admin area, you must include some libraries from within WordPress includes: // only need these if performing outside of admin environment require_once(ABSPATH . ‘wp-admin/includes/media.php’); require_once(ABSPATH . ‘wp-admin/includes/file.php’); require_once(ABSPATH . ‘wp-admin/includes/image.php’); // example image $image=”http://example.com/logo.png”; // magic sideload … Read more
I had similar problems sometime ago with a custom CSV import, but I ended up by using some custom SQL for the bulk insert. But I hadn’t seen this answer by then: Optimize post insert and delete for bulk operations? to use wp_defer_term_counting() to enable or disable term counting. Also if you check out the … Read more
Use wp_set_object_terms after you have the post id for each taxonomy: … $post_id = wp_insert_post( $my_post ); wp_set_object_terms( $post_id, $location, ‘location’ ); wp_set_object_terms( $post_id, $sale_rental, ‘sale_rental’ ); wp_set_object_terms( $post_id, $price, ‘price’ );
If you read the documentation for wp_insert_post, it returns the post ID of the post you just created. If you combine that with the following function __update_post_meta (a custom function I acquired from this site and adapted a bit) /** * Updates post meta for a post. It also automatically deletes or adds the value … Read more
You can set an image as post thumbnail when it is in your media library. To add an image in your media library you need to upload it to your server. WordPress already has a function for putting images in your media library, you only need a script that uploads your file. Usage: Generate_Featured_Image( ‘../wp-content/my_image.jpg’, … Read more