Hot answers tagged

68

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', $post_id ); ...


13

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' );


11

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 source for the WordPress importer plugin, you ...


10

You kind of answered the question yourself already, Create a function that will create the 3 posts ex: function create_new_user_posts($user_id){ if (!$user_id>0) return; //here we know the user has been created so to create //3 posts we call wp_insert_post 3 times. // Create post object ...


8

That's pretty strange issue which everyone will face especially if they call this function inside some loop <foreach> <for> <while> etc. You should try this if (!get_page_by_title($title, 'OBJECT', 'post') ){ $my_post = array('post_title' => $title, 'post_content' => 'Content', ...


8

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' => ...


8

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 validation functions can be used here $tags = array_map( 'esc_attr', $tags );


7

The short answer; absolutely. wp_insert_post() will only SQL escape the content. Use the KSES library & wp_kses() to filter out the nasties, or esc_html() to escape all HTML. Most importantly, check out the codex on data validation (read: sanitization). A Note On KSES: Use wp_filter_kses() or wp_kses_data() to apply the same KSES rules as post ...


7

Try using set_post_thumbnail(). Edit by Otto: You clarified your question, so I'll clarify the response Chip gave. Basically, you need to make the 'attachment' for the post as well. When an image is uploaded into the WordPress media library, a special post entry is made for it with a post type of attachment. This attachment is linked to some specific post ...


6

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'); ...


6

Here are some thoughts on the issue, but please note that this is by no means a conclusive answer as there may be some things I have overlooked however this should give you an insight to the potential gotchas. Yes, technically there could be consequences. Where calling wp_defer_term_counting(true) becomes truly beneficial is when for instance you are ...


5

You should first run the wp_insert_post() which will return the post ID. Then use that post ID to add your custom fields. Use add_post_meta() to add the custom fields. $post_id = wp_insert_post( $args ); add_post_meta( $post_id, 'longitude', $my_long ); add_post_meta( $post_id, 'latitude', $my_lat ); For image attachments, you can refer to this question: ...


5

You could use call kses_remove_filters() before saving and call kses_init_filters() afterwards, but pay attention it will also remove filtering from title, excerpt and comments, So what you should do is just unset the content filters. // Post filtering remove_filter('content_save_pre', 'wp_filter_post_kses'); remove_filter('content_filtered_save_pre', ...


5

Use some type of conditional tag to check if those posts exist or not. If they do not exist, have them created with wp_insert_post. Do this logic inside the AddMyPages function and not around the add_action function. Example You want to add a page with a parent ID only if it does not exist, and want this page to always exist. Since it's in the init hook, ...


5

you can use pre_post_update action hook like so: add_action('pre_post_update','post_updating_callback'); function post_updating_callback($post_id){ global $post; // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ...


4

When using function like wp_insert_post() It will work on the currently active blog, meaning that if you are on a blog in your network with blog_id of 2 the the post you insert using wp_insert_post() will be inserted to that blog. Now if you want to insert the post to a specific blog you can use switch to blog() function which will tell WordPress that ...


4

To save the link in the post meta you can use update_post_meta like this for example: $url = "http://sample.com/entertainment/default.aspx?tabid=2305&conid=102950" $my_post = array( 'post_title' => "$title", 'post_content' => "$content", 'post_status' => 'draft', 'post_author' => 1, 'post_category' => array(1), ); ...


4

You're looking for wp_delete_post. <?php $some_post_id = 1; wp_delete_post($some_post_id); The above will delete the post with ID 1 -- well, it will actually set it to a "trash" status. You can delete the post for real by setting the second parameter of wp_delete_post to true. <?php $some_post_id = 1; wp_delete_post($some_post_id, true); // really ...


4

Simply put the wp_insert_post call inside your conditional check so its only called if the post title is not empty, something like this: if (empty($_POST['my_title'])){ echo 'error: please insert a title'; } else { $title = $_POST['my_title']; $new_post = array( 'post_title' => $title, 'post_status' => 'publish', ...


4

You can do it using wp_insert_post, but you must specify taxonomy as well in tax_input, so it should look like this: $item['tax_input'] = array ( 'location' => implode( ',', $location ), 'sale_rental' => implode( ',', $sale_rental ), 'price' => implode( ',', $price ), ) I use implode() so that $location could be an ...


4

You get just the file names because you are not uploading the files. Uploading files using AJAX is currently not that easy. Newer browsers implement the FormData interface, but for older browsers you're stuck with some kind of flash uploader, like PlUpload for example. I suggest you use PlUpload because it's bundled with WP, and send all your data together ...


4

Right there is no big difference between them, actually wp_set_post_terms() uses wp_set_object_terms() but does few extra check for you. That's also noted on wp_set_object_terms() Codex page: Perhaps the wp_set_post_terms() is a more useful function, since it checks the values​​, converting taxonomies separated by commas and validating hierarchical ...


4

This part of wp_insert_posts() gives it away: if ( ! empty( $postarr['meta_input'] ) ) { foreach ( $postarr['meta_input'] as $field => $value ) { update_post_meta( $post_ID, $field, $value ); } } where we see how the post meta fields are updated/added with update_post_meta(). Here's the inline description for ...


4

Since you add a reference to my previous answer, let me share how I tested it: Setup on site A - XML-RPC Client include_once( ABSPATH . WPINC . '/class-IXR.php' ); include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); $client = new WP_HTTP_IXR_CLIENT( 'http://example.tld/xmlrpc.php' ); // <-- Change! $client->debug = true; $result = ...


3

335 is not an error message or code, it is the new post's ID that is retuned, so in short, the new post you have inserted was given the ID of 335 UPDATE wp_insert_post() already returns a WP_Error object on failure, so simply var_dump( $result ); would give you a specific error message on failure or the new post ID on success


3

It is not bug, actually WordPress does not allow (using arguments) to set post modification date. Internally WordPress set it to current time if you are updating an existing post else just set it to post date. in /wp-includes/post.php#L3192 you can see wp_insert_post does not use this argument if ( $update || '0000-00-00 00:00:00' == $post_date ) { ...


3

Capabilties - for guests? Your problem is that a guest doesn't have any capabilities. And when posts get processed, they need to pass certain checks. One is unfiltered_html. From Codex: Allows user to post HTML markup or even JavaScript code in pages, posts, comments and widgets. Note: Enabling this option for untrusted users may result in their ...


3

You will need to insert the post to get your ID but the $wpdb->postmeta table is very simple in structure. You could probably use a straight INSERT INTO statement, like this from the MySQL docs: INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); In your case... $ID = 1; // from your wp_insert_post $values = '($ID,2,3),($ID,5,6),($ID,8,9)'; // ...


3

It sure can be written in a more efficient way (untested): $added = array(); global $wpdb; foreach($uniques as $unique){ $pagetitle = getTitle($unique); $new_post = array( 'post_title' => $unique, 'post_status' => 'publish', 'post_type' => 'websites' ); $pid = wp_insert_post($new_post); if ($pid) { $wpdb->query( ...


3

I did not actually test your code thoroughly, but on the first glimpse: your form input is named post_category while you're grabbing $_POST['new_cat']. Adjusting that (e.g., set both to post_category) should already do. // EDIT And why are you grabbing $_POST['category'] in line 5?



Only top voted, non community-wiki answers of a minimum length are eligible